JVM new generation old generation

1. Why is there a young generation

We come and go again and again, why do we need to divide the heap? Couldn't generational be able to accomplish what he did? In fact, it is completely possible to not divide the generation. The only reason for the 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 have to find which objects are useless during GC, so that all areas of the heap will be scanned. And many of our objects are dying in the near future. If we are generational, we put the newly created objects in a certain place, and when GC is used, we will first recycle the area where the "dead in the future" objects are stored, so that It will free up a lot of space.

 

2. GC in the young generation

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

Because the objects in the young generation are basically dead (more than 80%), the garbage collection algorithm in the young generation uses the copy algorithm. The basic idea of ​​the copy algorithm is to divide the memory into two blocks, each time only use One of the blocks, when this block of memory is used up, copies the objects that are still alive 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 "To", while in the "From" area, surviving objects will decide where to go according to their age values. Objects whose age reaches a certain value (age threshold, which can be set by -XX:MaxTenuringThreshold) will be moved to the old generation, and objects that do not reach the threshold will be copied to the "To" area. After this GC, the Eden area and 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. Either way, 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 life 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 looks very similar to me. We played in the Eden area for a long time. One day there were too many people in the Eden area, so I was forced to go to the "From" area of ​​the Survivor area. Since I went to the Survivor area, I started to drift. Sometimes in the "From" area of ​​the Survivor area, there are At that time, I was in the "To" area of ​​Survivor and had no fixed place to live. Until I was 18 years old, my father said that I was an adult and it was time to go to the society. So I went to the old generation. There are many people in the old generation, and they are all very old. I also know a lot of people here. In the old generation, I live for 20 years (plus one year per GC) and then get recycled.

4. JVM parameters about the young generation

1)-XX:NewSize和-XX:MaxNewSize

It is 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 between Eden and one of the Survivors, this value is also more important.

3) -XX: + PrintTenuringDistribution

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

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

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

Guess you like

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