Prepare for 2022 spring move - java-day7

java

  1. X parameters of JVM (understand)
    • -Xint: Interpretation and execution
    • -Xcomp: compile into local code the first time you use it
    • -Xmixed: mixed mode
  2. XX parameter
    Note: check whether the parameter is used and check the parameter details: jinfo -flag parameter name process number
    Example: jinfo -flag MetaspaceSize 4488 (view the MetaspaceSize of process 4488)
    • Boolean type: - xx: + or - a property (+ on, - off)
      • Print GC collection details: - XX:-PrintGCDetails (- XX:+PrintGCDetails)
      • Whether to use serial garbage collector: - XX:-UseSerialGC (- XX:+UseSerialGC)
    • KV setting type: - xx: attribute key = attribute value
      • -20: Metaspacesize = 128M (Metaspace)
      • -20: Maxtenuringthreshold = 15 (this parameter is used to control how many times an object can go through Minor GC before it can be promoted to the previous generation. The default value is 15)
  3. -Xms and - Xmx
    • -Xms: equivalent to - XX:InitialHeapSize (initial heap memory)
    • -Xmx: equivalent to - XX:MaxHeapSize (maximum heap memory)
  4. View JVM defaults
    • -20: + printflagsinitial: mainly view the initial default
      • Formula: java -XX:+PrintFlagsInitial -version
    • -20: + printflagsfinal: mainly to view modifications and updates
      • Formula: java -XX:+PrintFlagsFinal -version
  5. Common parameters
    • -Xms: initial memory size. The default is 1 / 64 of physical memory
    • -Xmx: the maximum allocated memory is 1 / 4 of the physical memory by default
    • -Xss: set the size of a single thread stack, generally 512k~1024k by default
    • -Xmn: set the size of the younger generation
    • -20: Metaspacesize: sets the size of the Metaspace. The meta space is not in the virtual machine, but uses local memory. Therefore, by default, the size of the meta space is limited only by local memory
    • -20: + printgcdetails: output detailed GC collection log information
    • -20: Survivorratio: sets the proportion of eden and s0/s1 spaces in the Cenozoic
    • -20: Newratio: configure the proportion of the younger generation and the older generation in the heap structure. For example: - XX:NewRatio=2, the new generation accounts for 1, and the old generation accounts for 2
    • -20: Maxtenuringthreshold: sets the maximum age of garbage

algorithm

  • 209. Minimum length subarray
    Given an array containing n positive integers and a positive integer s, find the continuous sub array with the smallest length satisfying its sum ≥ s in the array, and return its length. If there is no eligible subarray, 0 is returned.

Example:

Input: S = 7, Num = [2,3,1,2,4,3] output: 2 explanation: subarray [4,3] is the subarray with the smallest length under this condition.

- Method 1: violent solution (double cycle)
- The first layer starts from the leftmost part of the loop, and the second layer starts from the first layer, recording the sum of the elements of each outer loop
- If the sum of elements>=target If so, record the length of the outer cycle to the inner cycle
- Select the minimum length each time,
- After the traversal, if the maximum value of the result is not reassigned, it will be 0, otherwise it will be the minimum length value of the reassignment
public int minSubArrayLen(int target, int[] nums) {
    int res = Integer.MAX_VALUE;
    int len;
    int sum;
    for (int i = 0; i < nums.length; i++) {
        sum = 0;
        for (int j = i; j < nums.length; j++) {
            sum += nums[j];
            if (sum >= target) {
                len = j - i + 1;
                res = Math.min(len, res);
                break;
            }
        }
    }
    return res == Integer.MAX_VALUE ? 0 : res;
}
- Method 2: slide the window.
- The so-called sliding window: it is to constantly adjust the starting position and ending position of the sub sequence, so as to get the result we want.
- To realize the sliding window, the following three points are mainly determined:
	- What's in the window? Meet its and>=s The smallest continuous subarray of length
	- How do I move the start position of the window? If the value of the current window is greater than s The window is about to move forward (that is, it's time to shrink)
	- How do I move the end position of the window? The end position of the window is the pointer to traverse the array, and the start position of the window is set as the start position of the array
public int minSubArrayLen(int target, int[] nums) {
    int res = Integer.MAX_VALUE;
    int len;
    int sum = 0;
    int i = 0;// Start position of sliding window
    for (int j = 0; j < nums.length; j++) {
        sum += nums[j];
        while (sum >= target) {
            len = j - i + 1;
            res = Math.min(res, len);
            // contract the window
            sum -= nums[i++];
        }
    }
    return res == Integer.MAX_VALUE ? 0 : res;
}

Keywords: Java Back-end

Added by burgessm on Mon, 24 Jan 2022 21:00:49 +0200