Heap space commonly used parameter settings

View the default initial values ​​of all parameters: -XX:PrintFlagsInitial
View the final values ​​of all parameters: -XX:+PrintFlagsFinal
Initial heap size: -Xms
heap maximum size: -Xmx or -XX:MaxHeapSize=size
New generation size: -Xmn or (-XX:NewSize=size + -XX:MaxNewSize=size)
Configure the ratio of the new generation to the old generation in the heap structure: -XX:NewRatio
set the ratio of Eden and s0/s1 space in the new generation: -XX: SurvivorRatio
Set the maximum age of the new generation garbage: -XX: MaxTenuringThreshould

Survivor ratio -XX:SurvivorRatio=ratio
promotion threshold -XX:MaxTenuringThreshold=threshold
promotion details -XX:+PrintTenuringDistribution
output detailed GC processing log: -XX:+PrintGCDetails

  • Print brief information: 1 :-XX:+PrintGC 2:verbose:gc
    FullGC before MinorGC -XX:+ScavengeBeforeFullGC

When the heap space overflows, a heapdump snapshot file is generated under the current project directory: -Xms8m -Xmx8m -XX:+HeapDumpOnOutOfMemoryError
Insert picture description here

Demonstrate memory allocation strategy


/**
 *  演示内存的分配策略
 */
public class Demo2_1 {
    
    
    private static final int _512KB = 512 * 1024;
    private static final int _1MB = 1024 * 1024;
    private static final int _6MB = 6 * 1024 * 1024;
    private static final int _7MB = 7 * 1024 * 1024;
    private static final int _8MB = 8 * 1024 * 1024;

    // -Xms20M -Xmx20M -Xmn10M -XX:+UseSerialGC -XX:+PrintGCDetails -verbose:gc -XX:-ScavengeBeforeFullGC
    public static void main(String[] args) throws InterruptedException {
    
    
        new Thread(() -> {
    
    
            ArrayList<byte[]> list = new ArrayList<>();
            list.add(new byte[_8MB]);
            list.add(new byte[_6MB]);
        }).start();

        System.out.println("sleep....");
        Thread.sleep(1000L);
    }
}

Insert picture description here
When the occupied memory exceeds the memory size of the eden area, a GC will be triggered. Objects that are still alive after multiple collections will be transferred to the old generation. If the object is too large, the eden area cannot be loaded, and the GC does not need to be triggered, but will be transferred directly to the old generation. Years

Guess you like

Origin blog.csdn.net/u014496893/article/details/114606891