当前位置:网站首页>Interviewer: why does the new generation memory need two survivor zones?

Interviewer: why does the new generation memory need two survivor zones?

2022-06-24 01:21:00 CodingCode

1 、Survivor Meaning of existence

Don't think about why there are two Survivor District , Imagine Survivor What is the meaning of the district ?

without Survivor,Eden Every time the district goes on Minor GC, The surviving object will be sent to the old age . In this way, the memory of the older generation will soon be used up , Trigger Major GC. Because the memory space of the old generation is much larger than that of the new generation , So do it once Full GC The ratio of time spent Minor GC Much longer , This will cause the system to execute slowly , Too slow to respond , The user experience is very bad , Not to mention that some connections may have connection errors due to timeout .

Let's think about it Survivor Under the circumstances , Is there any solution , This can be avoided :

  • Increase the memory of the older generation
    • The advantage is that it can bear more surviving objects , Reduce GC The frequency of
    • The disadvantages are also obvious , The bigger the space , happen GC It will take longer
  • Reduce old age memory
    • The advantage is GC Less time required
    • Weakness is GC The frequency increases

Obvious , No, Survivor Words , Neither of these solutions can fundamentally solve the problem .

So we can get the first conclusion :Survivor The meaning of being , Is to reduce the number of people sent to the old age , And then reduce Full GC Happen ,Survivor Pre screening guarantee , Only experience 16 Time Minor GC Objects that can survive in the new generation , Will be sent to the old age .

2 、 Why set up two Survivor

Set the two Survivor The biggest advantage of the zone is that it solves fragmentation

Why one Survivor Not in the district ? Suppose there is only one survivor District , Let's simulate the process :

Eden Full of , Trigger once Minor GC,Eden Live objects in will be moved to Survivor District . Continue the cycle , The next time Eden When full Minor GC,Eden and Survivor Each has some surviving objects , If you put Eden The survival objects of the zone are placed in Survivor District , Obviously, the memory occupied by these two parts of objects is not continuous , It also leads to memory fragmentation , Serious impact on system performance .

It makes sense , Two pieces should be built Survivor District , The newly created object is Eden in , Experience once Minor GC,Eden The live object in will be moved to the first piece survivor space S0,Eden Be emptied ; etc. Eden The district is full again , Just trigger it again Minor GC,Eden and S0 The live objects in will be copied to the second block survivor space S1( This process is very important , Because this replication algorithm guarantees S1 From S0 and Eden Two live objects occupy continuous memory space , To avoid fragmentation ).

S0 and Eden Be emptied , And then the next round S0 And S1 Exchange roles , And so on and so on . If the number of copies of an object reaches 16 Time , The object will be sent to the elderly .

The biggest advantage of the above mechanism is , The whole process , There will always be one survivor space It's empty. , The other is not empty survivor space No debris .

Last ad , If you think this article will help you , I can pay attention to my technical official account. 【CodingCode】. Your attention and forwarding is my biggest support ,O(∩_∩)O.

原网站

版权声明
本文为[CodingCode]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211118220302703E.html