JVM heap and heap tuning and how to troubleshoot OOM

Tuning location - heap

Heap, a JVM has only one heap memory, and the size of the heap memory can be adjusted.
After the class loader reads the class file, what will it usually put in the heap? Classes, methods, constants, variables ~, save the real objects of all our reference types; the heap
memory is also subdivided into three areas:
1. Newborn Zone (Zone 0/Zone 1): The place where classes are born, grow, and even die. Among them, the Eden area is where all new objects occur.
2. Retirement area: Objects that survived the GC from the new area enter the retirement area.
3. Permanent area: This area is resident in memory. It is used to store the Class object carried by DK itself. Interface metadata stores some environment or class information when Java is running. There is no garbage collection in this area! Turning off VM virtualization will release the memory in this area. The heap mainly stores loaded Class class-level objects such as

class itself, method , field, etc.
insert image description here
The surviving areas 0 and 1 are a dynamic change in the heap memory. It is a transition between the Eden area and the retirement area. As the name implies, objects that cannot survive will be "killed". GC garbage collection is mainly in the new area The Eden Park and the Elderly Care Area in China . The recycling methods are divided into light GC and heavy GC. The light GC mainly exists in the new area. Only when the garbage objects entering the old area exceed the limit will the heavy GC (full GC) be triggered. This operation mainly exists in the old area.
insert image description here
You may encounter OOM problems during development, indicating that there is not enough memory. This memory is also the heap memory of the JVM, such as the following example:
Exception in thread “main” java.lang.OutOfMemoryError:
insert image description here
99% of the objects in the Java heap space are temporary Object, so after GC, it rarely enters the old area, so OOM rarely happens

Evolution of the permanent zone

This area is resident in memory. It is used to store the Class object carried by IDK itself. Interface metadata, which stores some environment or class information of the Java runtime.

Before jdk1.6: permanent generation, the constant pool is in the method area;
jdk1.7: permanent generation, but slowly degenerates, go to the permanent generation, the constant pool is in the heap
After jdk1.8: no permanent generation, the constant pool is in Metaspace

insert image description here

The emergence of OOM

The case of memory corruption in the permanent area: a startup class loads a large number of third-party jar packages. Tomcat deploys too many applications and a large number of dynamically generated reflection classes. Constantly being loaded. OOM will appear until the memory is full;
insert image description here
by default: the total memory allocated is one-fourth of the computer memory, and the initialized memory is one-sixty-fourth of the computer memory,
but these memory parameters can be manually adjusted to
insert image description here
run the program After that, it is found that the JVM parameters have changed
insert image description here

It can be seen that metaspace exists logically but not physically

How to troubleshoot OOM

Objects can be analyzed using memory snapshot tools, mainly: MAT (Eclipse) and Jprofile (IDEA).
Just pass the following parameters to the VM.
insert image description here
When the program runs and OOM occurs, go to the file directory where the class is located to find the hprof file generated by Jprofile.
insert image description here

Double-click to open it to initially find objects that occupy a large amount of memory, commonly known as "large objects"
insert image description here

View the thread (Thread Dump) to find the line where the problem occurs in the program.
insert image description here
According to the specific situation, you can tune the JVM related tuning parameters:
insert image description here
Share some other JVM parameters:
-Xmx4g: The maximum heap memory is 4GB.
-Xms4g: Initialize the heap memory size to 4GB.
-Xmn1200m: Set the young generation size to 1200MB . After increasing the young generation, the size of the old generation will be reduced. This value has a great impact on system performance, and Sun officially recommends configuring it as 3/8 of the entire heap.
-Xss512k: Set the stack size per thread . After JDK5.0, the stack size of each thread is 1MB, and the stack size of each thread was 256K before. It should be adjusted according to the memory size required by the application thread. Under the same physical memory, reducing this value can generate more threads. However, the operating system still has a limit on the number of threads in a process, and it cannot be generated infinitely. The experience value is around 3000~5000.
-XX:NewRatio=4: Set the ratio of the young generation (including Eden and two Survivor areas) to the old generation (excluding the permanent generation) . If it is set to 4, the ratio of the young generation to the old generation is 1:4, and the young generation occupies 1/5 of the entire stack.
-XX:SurvivorRatio=8: Set the size ratio of the Eden area to the Survivor area in the young generation . 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/10 of the entire young generation.
-XX:PermSize=100m: Initialize the permanent generation size to 100MB .
-XX:MaxPermSize=256m:Set the persistent generation size to 256MB .
-XX:MaxTenuringThreshold=15: Set the garbage maximum age . If it is set to 0, the young generation object will not pass through
the adjustable parameters:
-Xms: initialize the heap memory size, the default is 1/64 of the physical memory (less than 1GB) .
-Xmx: **The maximum value of heap memory. **By default (the MaxHeapFreeRatio parameter can be adjusted) when the free heap memory is greater than 70%, the JVM will reduce the heap until the minimum limit of -Xms.
-Xmn: The size of the new generation, including the Eden area and 2 Survivor areas .
-XX:SurvivorRatio=1: The ratio of Eden area to a Survivor area is 1:1 .
-XX:MaxDirectMemorySize=1G: ** direct memory. **Report java.lang.OutOfMemoryError: Direct buffer memory exception can increase this value.
-XX:+DisableExplicitGC: Prohibit runtime from explicitly calling System.gc() to trigger fulll GC .
Note: Java RMI's timing GC trigger mechanism can be configured to control the trigger time by configuring -Dsun.rmi.dgc.server.gcInterval=86400.
-XX:CMSInitiatingOccupancyFraction=60: Old age memory recovery threshold, the default value is 68 .
-XX:ConcGCThreads=4: CMS garbage collector parallel threads, the recommended value is the number of CPU cores .
-XX:ParallelGCThreads=8: The number of threads for the new generation parallel collector .
-XX:MaxTenuringThreshold=10: Set the garbage maximum age . If it is set to 0, the young generation objects will directly enter the old generation without going through the Survivor area. For applications with more old generations, efficiency can be improved. If this value is set to a larger value, the young generation object will be copied multiple times in the Survivor area, which can increase the survival time of the object in the young generation and increase the probability of being recycled in the young generation.
-XX:CMSFullGCsBeforeCompaction=4: After specifying how many times fullGC is performed, the memory space in the tenured area is compressed .

-XX:CMSMaxAbortablePrecleanTime=500: When the execution of the abortable-preclean precleaning phase reaches this time, it will end .

Guess you like

Origin blog.csdn.net/weixin_57535055/article/details/129232440