1. Judge whether the object is alive
To reclaim memory, you first need to judge , That memory needs to be recycled . That is, we need to judge which objects are still alive , Then these don't need to be recycled .
(1) Reference counting
principle ： Object to add a reference counter . If you are quoted, you will accumulate . Then the value in the counter is greater than 0, Is still quoted , Can't be recycled .
shortcoming ： Can't solve the case of circular references .
(2) Accessibility analysis
principle ： From some known as GCRoots The object node of the , Start searching down , Judging the survival of an object according to whether it can reach or not . among , from GCRoots The path between an object and a target object is called a reference chain .
GCRoots The objects of include the following ：JAVA The reference object in the local variable table in the virtual stack 、 Objects referenced in the local method stack 、 Object referenced by a static property in a method area 、 Method area , Objects referenced by constants .
2. Garbage collection algorithm
(1) Mark - Elimination method
principle ： First mark the objects that need to be recycled , After the completion of re labeling, all marked objects will be recycled .
shortcoming ： efficiency 【 Labeling and recycling are not efficient 】、 Space 【 Just recycle the objects that need to be recycled , There's no sorting out of the space system , therefore , There will be a lot of memory fragmentation , It's not easy to use 】
(2) Mark - Replication
principle ： Divide the memory into two parts S1 and S2, Use part of it at a time , such as S1. etc. S1 When the space is full , Put the unmarked 【 Living objects 】 Copy to another memory space S2 in , Recycle the whole S1.
shortcoming ： Memory usage is only half of the original . If the survival rate is high , The efficiency of this algorithm will be reduced .
(3) Mark - Finishing method
principle ： First mark the object that needs to be deleted . Then move all surviving objects to one side . After that, clear the memory outside the end .
(4) Generation by generation collection
Depending on the memory lifetime , Divide it into different pieces .JAVA The reactor is generally divided into the new generation and the old generation . According to the characteristics of their respective ages , Choose the appropriate collection algorithm . such as ： The survival rate of the new generation is low , therefore , The new generation chooses the replication algorithm . The older generation has a higher survival rate , There's no extra space to guarantee it , It is necessary to adopt “ Mark - eliminate ” Algorithm or “ Mark - clear ” Algorithm for recycling .
① The next generation of recycling algorithms
1) Cenozoic memory according to 8:1:1 The proportion of eden Area and two survivor (survivor0,survivor1) District . One Eden District , Two Survivor District ( generally speaking ). Most objects in Eden In the region . Recycle first eden Area survival object copied to a survivor0 District , And then empty eden District , When this survivor0 The area is also full , Will eden Area and survivor0 Section live object is copied to another survivor1 District , And then empty eden And this survivor0 District , here survivor0 The zone is empty , And then survivor0 Area and survivor1 Zone Exchange , That is to keep survivor1 The area is empty , So back and forth .
2) When survivor1 Insufficient storage area eden and survivor0 Of the live object , The living objects are stored directly into the old age .
3) If the old age is full, it will trigger once Full GC, The Cenozoic era 、 Older generations recycle .
② Recycling in the old days is big
1) In the younger generation N Objects that are still alive after secondary garbage collection , Will be put into the old generation . therefore , It can be thought that the older generation is storing objects with a longer life cycle .
2) Memory is much larger than the new generation ( The approximate ratio is 1:2), Trigger when old age memory is full Major GC namely Full GC,Full GC It happens less frequently , Older generations live longer , High survival marker .
③ Permanent generation recycling algorithm
3. Garbage collector
Hotspot The included garbage collector .
The garbage collector can be classified into three types according to the running situation of threads
① Serial recovery ：serial Recyclers , Single thread recycling . The entire stw(Stop The World).
② Parallel recycling ： Names to parallel The recycler at the beginning 【parallel scavenge Recyclers 、parallel old Recyclers 】, Multithreading recycling . The entire (Stop The World).
③ Concurrent recovery ：CMS and G1, Multi thread staged recycling . Only at some stage will (Stop The World).
(1) Serial The collector （ Copy algorithm )
A new generation of single-threaded collectors , Marking and cleaning are both single threaded , The advantage is simplicity and efficiency .
(2) Serial Old The collector ( Mark - Sorting algorithm )
Old single thread collector ,Serial Old age version of collector .
(3) ParNew The collector ( stop it - Copy algorithm )
Cenozoic collector , Think of it as Serial Multithreaded version of collector , In multicore CPU The environment has a ratio Serial Better performance .
(4) Parallel Scavenge The collector ( stop it - Copy algorithm )
Parallel collector , Pursuing high throughput , Efficient use of CPU.
(5) Parallel Old The collector ( stop it - Copy algorithm )
Parallel Scavenge Old age version of collector , Parallel collector , Throughput priority .
(6) CMS(Concurrent Mark Sweep) The collector （ Mark - Clean up algorithm ）
A. Only the old generation and the permanent generation will be recycled , Not recycling young people .
B. CMS It's a pretreatment garbage collector , It can't wait for old Recycle when memory runs out . You need to recycle before you run out of memory . Otherwise, the concurrent collection will fail . therefore ,CMS The garbage collector has a trigger threshold at the start of its operation , Default to old age or permanent generation 92%.
CMS be based on “ Mark - eliminate ” algorithmic . Is the most commonly used garbage collector .CMS How garbage collectors work is 7 A step
1． Initialization tag , It can lead to stw
2． Concurrent Tags , Run with user thread .
3． Pre cleaning - Run with user thread .
4． Pre cleaning that can be terminated , Run with user thread
5． Re label , It can lead to swt
6． Concurrent elimination , Run with user thread .
7． Concurrent reset state wait for next time CMS Trigger , Run with user thread .
CMS The operation process is as follows ：
4. GC Types and triggers
Because objects are generational , So the garbage collection area 、 Time is different .GC There are two types of ：Scavenge GC and Full GC.
(1) Scavenge GC
General situation , When you create a new object , Yes Eden When applying for space fails , Will trigger Scavenge GC. eliminate Eden The living object of Central Africa , Move the live object to S1 in . Then I'll sort out two S District . such GC It doesn't affect the older generation . Yes Eden GC The frequency is higher , therefore , Need more efficient algorithm 【 Copy algorithm 】.
(2) Full GC
Clean up the entire heap . Including the new generation , Old age . So than Scavenge GC It takes a long time . therefore , It needs to be as little as possible FULL GC The number of times .
Triggering scenarios ： Old age is full of （Tenured）、 The enduring generation is full of （Perm）、System.GC Called by display .
5. Memory allocation and recycling strategies
When objects are allocated , You need to choose an unused space . At the same time, two threads need to create objects . therefore , There may be concurrency conflicts . There are two solutions . firstly ： When you allocate space , Ensure that only one thread is in the application space at the same time . second ： Each thread gets an area in the heap . Used to create the thread's own object .Thread Local Allocation Buffer, abbreviation TLAB.
(1) Priority is given to Eden The distribution of
(2) Large objects go directly into the old generation
Set parameters , The application space is greater than the set threshold , It's created directly in the older generation .
(3) Long term survivors enter the old age
According to the age threshold set for promotion to the old age , Object exceeds the set threshold , It's moving back to the old days .
(4) Dynamic object age determination
Select the age threshold according to the proportion . The sum of the size of objects of the same age is greater than or equal to the whole S Half of , Then the current age is the threshold . Objects older than or equal to this age can be moved to the old age , It doesn't have to be compared to the age threshold .
(5) Space allocation guarantee
When the new generation can't allocate memory , Transfer the new generation to the old , And then put the new object in the new generation of the air .
If the older generation can't guarantee 【 There is a continuous space greater than S The sum of all objects in the zone --S You can move it all over 】 The failure of the guarantee will cause Full GC. We need to avoid frequent occurrence as much as possible Full GC. therefore , Need to understand space allocation guarantees .