Setting and viewing the size of heap space

1. Set the parameters of the heap space size

-Xms is used to set the initial memory size of the heap space (young generation + old generation)
-X is the jvm operating parameter
ms is memory start
-Xmx is used to set the maximum memory size of the heap space (young generation + old generation)

2. The size of the default heap space

Initial memory size: physical computer memory size 1/64
Maximum memory size: physical computer memory size 1/4

3. Manual setting: -Xms600m -Xmx600m

It is recommended to set the initial heap memory and the maximum heap memory to the same value during development.

4. View the set parameters:

Method 1: jps to view the process number, jstat -gc process id
Method 2: Add heap parameters to print detailed information -XX:+PrintGCDetails

Here we demonstrate the second way
Insert picture description here

public class HeapSpaceInitial {
    
    
    public static void main(String[] args) {
    
    

        //返回Java虚拟机中的堆内存总量
        long initialMemory = Runtime.getRuntime().totalMemory() / 1024 / 1024;
        //返回Java虚拟机试图使用的最大堆内存量
        long maxMemory = Runtime.getRuntime().maxMemory() / 1024 / 1024;

        System.out.println("-Xms : " + initialMemory + "M");
        System.out.println("-Xmx : " + maxMemory + "M");

//        System.out.println("系统内存大小为:" + initialMemory * 64.0 / 1024 + "G");
//        System.out.println("系统内存大小为:" + maxMemory * 4.0 / 1024 + "G");
    }
}

Print result:

Insert picture description here

Comparison of 7 classic garbage collection periods

Insert picture description here

How to choose a garbage collector

1. Prioritize the adjustment of the heap size so that jvm's self-adaptation is completed.
2. If the memory is less than 100M, use a serial collector
3. If it is a single-core, stand-alone program, and there is no pause time requirement, serial collector (Serial)
4. If it is a multi-core cpu, high throughput is required. Allow the pause time to exceed 1 second, choose parallel jvm (Parallel GC)
5. If it is a multi-cpu, the pursuit of low pause time requires fast response (for example, the delay cannot exceed 1 second, such as Internet applications), use a concurrent collector (CMS), But the official recommendation is to use G1, which has high performance. Current Internet projects basically use G1

Guess you like

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