In-depth understanding of JVM (3) - configuration parameters

There are three types of JVM configuration parameters:

1. Tracking parameters

2. Heap allocation parameters

3. Stack allocation parameters

These three types of parameters are used to track and monitor the JVM state, allocate heap memory and allocate stack memory.

tracking parameters

Trace parameters are used to track and monitor the JVM, and are often used by developers for JVM tuning and troubleshooting.

1. When GC occurs, print GC brief information

Use -XX:+PrintGC or -verbose:gc parameter

These two configuration parameters have the same effect, both print out brief information when GC occurs, such as executing code:

1: public static void main(String[] args) 2: { 3: byte[] bytes =null; 4: for(int i=0;i<100;i++){ 5: bytes = newbyte[1 * 1024 * 1024]; 6: } 7: }

This program creates 100 1M array objects continuously. Use the -XX:+PrintGC or -verbose:gc parameter to execute the program to check the GC situation:

1: [GC (Allocation Failure) 32686K->1648K(123904K), 0.0007230 secs] 2: [GC (Allocation Failure) 34034K->1600K(123904K), 0.0009652 secs] 3: [GC (Allocation Failure) 33980K->1632K(123904K), 0.0005306 secs]

We can see that the program executes 3 GCs (minor GC), these three GCs are all new generation GCs, because every time this program creates a new array object, it will assign the new object to the bytes variable, while the old object Without any object referencing it, the old pair of objects will become unreachable, and these unreachable objects will be collected during the minor GC of the young generation.

32686K indicates the space occupied by the object before being reclaimed. 1648K means that the object takes up space after recycling. 123904K indicates how much space is still available. 0.0007230 secs represents how long this garbage collection took.

2. Print GC details and heap usage details

Use the -XX:+PrintGCDetails parameter

1: [GC (Allocation Failure) [PSYoungGen: 32686K->1656K(37888K)] 32686K->1664K(123904K), 0.0342788 secs] [Times: user=0.00 sys=0.00, real=0.03 secs] 2: [GC (Allocation Failure) [PSYoungGen: 34042K->1624K(70656K)] 34050K->1632K(156672K), 0.0013466 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 3: Heap 4: PSYoungGen total 70656K, used 43118K [0x00000000d6100000, 0x00000000dab00000, 0x0000000100000000) 5: eden space 65536K, 63% used [0x00000000d6100000,0x00000000d8985ac8,0x00000000da100000) 6: from space 5120K, 31% used [0x00000000da600000,0x00000000da796020,0x00000000dab00000) 7: to space 5120K, 0% used [0x00000000da100000,0x00000000da100000,0x00000000da600000) 8: ParOldGen total 86016K, used 8K [0x0000000082200000, 0x0000000087600000, 0x00000000d6100000) 9: object space 86016K, 0% used [0x0000000082200000,0x0000000082202000,0x0000000087600000) 10: Metaspace used 2669K, capacity 4486K, committed 4864K, reserved 1056768K 11: class space used 288K, capacity 386K, committed 512K, reserved 1048576K

We see that in addition to printing GC information, the heap usage is also displayed, and the heap is divided into young generation, old generation, and metaspace. Note that there is no permanent area here. The permanent area has been removed in java8. The constants and string static variables originally placed in the permanent area have been moved to the metaspace and use local memory.

The new generation is divided into Eden area (eden) and survivor area (from and to). From the content printed above, you can see that the total size of the new generation is 70656K, and 43118K is used. Careful students may find eden+from +to=65536K+5120K+5120K=75776 does not equal the total size of 70656K, why is this? This is because the garbage collection algorithm of the new generation adopts the copy algorithm, which simply means copying back and forth between from and to (the unreachable objects are collected during the copying process), so it must be ensured that one of the areas is empty. Only in this way can there be reserved space to store the copied data, so the total size of the new generation is actually equal to eden+from (or to)=65536K+5120K=70656k.

3. Use external files to record GC logs

There is also a very useful parameter, which can record the GC log to an external file, which is especially important when troubleshooting in the production environment. When the java program appears OOM, you always want to see the garbage collection situation at that time. Through this The parameters can record the GC log, which is convenient for troubleshooting, and of course, it can also be used for daily JVM monitoring.

-Xloggc:log/gc.log

R)A5CI(P(4X50Y}}NVJSTTU

4. Monitoring class loading

-XX:+TraceClassLoading

Use this parameter to monitor the classes loaded by the java program:

PF}FPBLI4UP~GMKVNKGS10L

Heap configuration parameters

Specify the maximum heap, the minimum heap: Xmx, Xms

These two parameters are the most familiar and commonly used parameters. You can use the following code to print out the current memory usage:

1: public static void main(String[] args) 2: { 3: System.out.println("最大堆:"+Runtime.getRuntime().maxMemory()/1024/1024+"M"); 4: System.out.println("空闲堆:"+Runtime.getRuntime().freeMemory()/1024/1024+"M"); 5: System.out.println("总的堆:"+Runtime.getRuntime().totalMemory()/1024/1024+"M"); 6: }

The maximum heap is the size specified by the Xmx parameter, which indicates the maximum memory size that the java program can use. If it exceeds this size, the java program will report: out of memory

 

(OOM error), the free heap indicates the memory size that the program has allocated minus the memory size that has been used, and the total heap indicates how much memory the program has currently configured. Generally speaking, when the program starts, it will first allocate 5M according to -Xms5m space, then the total heap size is 5M.

Specify the new generation memory size: Xmn, for example, we specify -Xmx20m -Xms5m -Xmn2m -XX:+PrintGCDetails

1: 最大堆:19.5M 2: 空闲堆:4.720428466796875M 3: 总的堆:5.5M 4: Heap 5: PSYoungGen total 1536K, used 819K [0x00000000ffe00000, 0x0000000100000000, 0x0000000100000000) 6: eden space 1024K, 79% used [0x00000000ffe00000,0x00000000ffeccc80,0x00000000fff00000) 7: from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000) 8: to space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000) 9: ParOldGen total 4096K, used 0K [0x00000000fec00000, 0x00000000ff000000, 0x00000000ffe00000) 10: object space 4096K, 0% used [0x00000000fec00000,0x00000000fec00000,0x00000000ff000000) 11: Metaspace used 2723K, capacity 4486K, committed 4864K, reserved 1056768K 12: class space used 293K, capacity 386K, committed 512K, reserved 1048576K

It can be seen that the total size of the new generation is eden+from+to=1024k+512k+512k=2M, which corresponds to the -Xmn we set.

 

The ratio of the young generation (eden+from+to) to the old generation (excluding the permanent area): -XX:NewRatio

For example, we set the parameters: -Xmx20m -Xms20m -XX:NewRatio=4 -XX:+PrintGCDetails (note that changing the parameter to 4 means that the ratio of the new generation to the old generation is 1:4)

1: 最大堆:19.5M 2: 空闲堆:8.665084838867188M 3: 总的堆:19.5M 4: Heap 5: PSYoungGen total 3584K, used 916K [0x00000000ffc00000, 0x0000000100000000, 0x0000000100000000) 6: eden space 3072K, 29% used [0x00000000ffc00000,0x00000000ffce52f8,0x00000000fff00000) 7: from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000) 8: to space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000) 9: ParOldGen total 16384K, used 10240K [0x00000000fec00000, 0x00000000ffc00000, 0x00000000ffc00000) 10: object space 16384K, 62% used [0x00000000fec00000,0x00000000ff600010,0x00000000ffc00000) 11: Metaspace used 2723K, capacity 4486K, committed 4864K, reserved 1056768K 12: class space used 293K, capacity 386K, committed 512K, reserved 1048576K

 

It can be seen that the new generation: eden+from+to=3072+512+512=4096k, the old generation: 16384k, the new generation: the old generation=4096k: 16384k=1:4 and -XX:NewRatio=4 match.

The size ratio of the Eden area and the Survivor area (from, to): -XX:SurvivorRatio (if it is set to 8, the ratio of two Survivor areas to one Eden area is 2:8, and one Survivor area accounts for 1/1 of the entire young generation. 10)

For example, set the parameters -Xmx20m -Xms20m -Xmn8m -XX:SurvivorRatio=6 -XX:+PrintGCDetails

This parameter sets the new generation memory size to 8m, and sets the ratio of the Survivor area to an Eden area to 2:6. Let's take a look at the print information:

1: 最大堆:19.0M 2: 空闲堆:8.104576110839844M 3: 总的堆:19.0M 4: Heap 5: PSYoungGen total 7168K, used 1040K [0x00000000ff800000, 0x0000000100000000, 0x0000000100000000) 6: eden space 6144K, 16% used [0x00000000ff800000,0x00000000ff904090,0x00000000ffe00000) 7: from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000) 8: to space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000) 9: ParOldGen total 12288K, used 10240K [0x00000000fec00000, 0x00000000ff800000, 0x00000000ff800000) 10: object space 12288K, 83% used [0x00000000fec00000,0x00000000ff600010,0x00000000ff800000) 11: Metaspace used 2723K, capacity 4486K, committed 4864K, reserved 1056768K 12: class space used 293K, capacity 386K, committed 512K, reserved 1048576K

Survivor area=from+to=2048, Eden area=6144K, Survivor area:Eden area=2:6, which is consistent with -XX:SurvivorRatio=6.

There are other two parameters -XX:+HeapDumpOnOutOfMemoryError, -XX:+HeapDumpPath, which can print the stack information to an external file when an OOM exception occurs.

Summary of Heap Allocation Parameters

Adjust the size of the young and survivor generations according to the actual situation

It is officially recommended that the new generation account for 3/8 of the heap

The surviving generation accounts for 1/10 of the young generation

During OOM, remember to dump the heap to ensure that you can troubleshoot on-site problems

Permanent Area Allocation Parameters

-XX:PermSize -XX:MaxPermSize

It is used to set the initial space and maximum space of the permanent area. They indicate how many types a system can accommodate, and the general space is relatively small. After java1.8, the permanent area was moved to the metadata area, using local memory, so these two parameters are no longer recommended.

stack size allocation parameter

The stack size parameter is -Xss, usually only a few hundred k, which determines the depth of function calls. Each thread has its own independent stack space. If the function call is too deep and exceeds the stack size, a java.lang.StackOverflowError will be thrown. Usually we encounter this kind of error. Instead of adjusting the -Xss parameter, we should investigate the reason why the function call is too deep. Using recursion, can you guarantee recursive exit, etc.

summary

This article explains the commonly used parameters of JVM, involving the allocation of trace, heap, permanent area, and stack. The most important and most commonly used are the allocation parameters of trace and heap, which are also closely related to tuning and troubleshooting.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325262013&siteId=291194637