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:
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:
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
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
4. Monitoring class loading
-XX:+TraceClassLoading
Use this parameter to monitor the classes loaded by the java program:
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:
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
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)
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:
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.