day90-performance stress test-performance monitoring-heap memory and garbage collection

The following two pictures understand that it is not our focus here. In order to introduce the heap, the heap is a key object of our memory tuning.

All object instances and arrays are allocated on the heap. The heap is the main area managed by the garbage collector, also known as the GC heap; it is also the place we consider most for optimization .

The heap can be divided into

JVM structure diagram, focusing on the heap

The highlight is coming

 

The figure below shows the general process, the new generation on the left and the old generation on the right

First understand the two concepts:

Minor GC : Aiming at the Eden Park GC in the new generation, it takes a relatively short time to remove useless objects in it

Full GC  clears all useless objects in the heap space (including the young generation and the old generation), and the cleaning is much longer than the minorGC, which greatly affects performance. Avoid frequent occurrences.

 

Speaking of garbage collection, it is actually only concerned with the heap space. Let's talk about it below:

Talk about the young generation of JVM

1. Why are there young generations

We have come repeatedly first, why do we need to divide the heap into generations? Can't he accomplish what he did regardless of generations? In fact, it is completely possible to not split the generation, the only reason for generation is to optimize the GC performance. Think about it first, if there is no generation, then all our objects are in one piece, and we need to find which objects are useless during GC, so that all areas of the heap will be scanned. And many of our objects are living and dying day and night. If we split the generations, we put the newly created object in a certain place. When GC, the area where the "life and dying" object is stored first is recycled, so It will free up a lot of space.

 

2. GC in the young generation

    HotSpot JVM divides the young generation into three parts: 1 Eden area and 2 Survivor areas (called from and to respectively). The default ratio is 8:1, why the default is this ratio, we will talk about it next. Under normal circumstances, newly created objects will be allocated to the Eden area (some large objects are treated specially). After the first Minor GC, if these objects are still alive, they will be moved to the Survivor area. Each time an object survives a Minor GC in the Survivor area, its age will increase by 1 year, and when its age increases to a certain level, it will be moved to the old generation.

Because the objects in the young generation are basically living and dying (more than 80%), the garbage collection algorithm in the young generation uses a copy algorithm. The basic idea of ​​the copy algorithm is to divide the memory into two blocks, and only use each time. One of the blocks, when this block of memory is used up, the surviving objects are copied to the other block. The copy algorithm does not generate memory fragmentation.

At the beginning of the GC, the object will only exist in the Eden area and the Survivor area named "From", and the Survivor area "To" is empty. Immediately after the GC, all surviving objects in the Eden area will be copied to the "To", and in the "From" area, the surviving objects will decide where to go based on their age. Objects whose age reaches a certain value (the age threshold can be set by -XX:MaxTenuringThreshold) will be moved to the old generation, and objects that have not reached the threshold will be copied to the "To" area. After this GC, the Eden area and the From area have been emptied. At this time, "From" and "To" will exchange their roles, that is, the new "To" is the "From" before the last GC, and the new "From" is the "To" before the last GC. In any case, the Survivor area named To is guaranteed to be empty. Minor GC will repeat this process until the "To" area is filled, and after the "To" area is filled, all objects will be moved to the old generation.

3. The lifetime of an object

I am an ordinary java object. I was born in the Eden area. In the Eden area, I also saw a little brother who looked like me. We played in the Eden area for a long time. One day there were too many people in the Eden area, and I was forced to go to the "From" area in the Survivor area. Since I went to the Survivor area, I started to drift. Sometimes in the "From" area in Survivor, there are When I was in the "To" area of ​​Survivor, I didn't have a fixed home. Until I was 18 years old, my father said that I was an adult and that I should go to the society to break into the world. So I went to the old generation. In the old generation, there are a lot of people, and they are all very old. I also know a lot of people here. In the old generation, I lived for 20 years (each GC plus one year), and then I was recycled.

4. JVM parameters about the young generation

1)-XX:NewSize和-XX:MaxNewSize

Used to set the size of the young generation, it is recommended to set it to 1/3 or 1/4 of the entire heap size, and the two values ​​are set to the same size.

2)-XX:SurvivorRatio

Used to set the ratio of Eden to one of Survivor, this value is also more important.

3) -XX: + PrintTenuringDistribution

This parameter is used to display the size of the objects of each age group in the Survivor area each time Minor GC.

4).-XX:InitialTenuringThreshol和-XX:MaxTenuringThreshold

It is used to set the minimum and maximum age of the objects promoted to the old generation. After each object persists in a Minor GC, the age is increased by 1.

 

Guess you like

Origin blog.csdn.net/JavaCoder_juejue/article/details/112976175