Old age GC：FullGC It's old GC, In the Cenozoic era, if there are existing objects or newly created objects
The objects that come out need to be moved to the older generation for some reasons , However, the root does not have such a large memory space to accommodate this object ,
Then it will trigger a FullGC, If after execution FullGC after , There is still no way to allocate memory for these objects , It's so cold , It's time to throw an exception , The exception type is OutOfMemoryError.
FullGC harm ： In occurrence FULL GC When , signify JVM It will safely suspend all executing threads (Stop The
World), To reclaim memory space , During this time period , All threads except garbage collection , Other related JAVA Procedure , The code will stand still , Reflected on the system , There will be a significant slowdown in the system response , Card machine status .
and FullGC And are used MinorGC Different algorithms , It uses a mark removal algorithm , Listen to the name , I understand very well , Analysis of incoming wave diagram . Deepen understanding JVM The diagram in a book looks like this ,
If you look at the name, mark it first , Then delete . This is also the most basic algorithm . This algorithm is divided into two steps
• sign （Mark） process ： Find all accessible objects , Make a specified mark .
• eliminate （Swep） process ： Traverse heap memory , Recycle unmarked objects .
After knowing this , We have to say another concept , That's it GC Root,Root We can understand it as a root node, like this
In the figure above a,b,c,d, Is the living object , If this reference exists , for instance b Referenced a, that a He belongs to the living object . When there is not enough available memory space in the memory area of our older generation , Then the whole world will be quiet （stop
the world）, It's time to start preparing for garbage collection .
• sign ： Traverse all GC Roots, Then all GC
Roots Reachable objects are marked as living objects . That's what we marked in the figure a,b,c,d.• eliminate ： The cleanup process traverses all objects in the heap , Clear all unmarked objects . in other words , If there is not enough memory ,GC The thread is triggered and the program is suspended , Then mark the objects that are still alive , Finally, all unmarked objects in the heap are cleared , Then let the program continue to run .
This is what the flow chart looks like Object state in the old generation under initial
At this time, it is not marked , Next, there is not enough memory ,GC Thread stop , Start marking
Start traversal according to the root node Marked abcdeh Are living objects , Next, start marking .
The next step is to clear the data , This is even simpler
When it's clear, there's also the removal of the mark , You can continue to clear the mark the next time
In this way, the mark clearing is completed , There are two more things to say ,
One is why the program should be stopped when the marking algorithm is clear ,（stop the world）.
Second, what are the advantages and disadvantages of mark removal algorithm ?（Stop the World）
Program stop is actually understandable , Because if you don't stop the program , We're marking this b After object , We new Create a new object J, Can from B point J, in other words , At this time J It should be marked , But the reality is certainly not , This object is in B After marking , It's almost over , We also new Come up with an object , as one can imagine , He must be unmarked , So when cleaning in the second stage , This miserable J Will be cleared , That's definitely not in line with our actual situation .
You think it's just new The resulting object was cleared , Suddenly it becomes null , That doesn't meet our requirements . So he'll stop the program first , Then it won't happen again , Then proceed to the start marking phase .
Advantages and disadvantages
First of all, we can look at the shortcomings , His shortcomings are very obvious ,
• Because he can iterate recursively Root, In that case Stop the
World It takes a long time , It's not so easy to wait like this .• The second is that the memory space cleared by this clearing method is discontinuous , Look at this picture
Death is divided up and down 2 part , Is discontinuous , Here JVM It creates an additional burden , He needs to maintain a free list of memory , If we go at this time new An array , Think about him looking for this continuous memory space , Will it be much more difficult ?
He also has advantages ,
• For example, there will be no circular references , We can think about it Two classes Cross reference ,A in newB,B in newA, Isn't that right a.b=b ,b.a = a , Is that so?
, And after the mark removal algorithm is finished , Yes, it can be recycled a, and b of , Because he traverses the tag from the root element , That is, from ab start , So single a And single b It's not marked , therefore , This avoids the problem of circular references • Another feeling is no difference , All references are made when there is not enough memory . There's nothing to say .
sign -- Sorting algorithm
And because of the tag -- The clearing algorithm will lead to various uneven spaces in memory allocation , Then there is another algorithm , Mark the living objects directly , Then connect him to the memory space boundary , Then he cleared the rest directly . This method is clearly illustrated , The rest are the same as the mark removal algorithm , There seems to be no explanation , Directly above