Garbage collection (Garbage Collection, GC) is one of the important features of Java that differs from C and C + +.
He helps Java automatically empty objects that are no longer used in the heap.
Because there is no need to manually free up memory, programmers can reduce the chance of making mistakes in programming.
With garbage collection, programmers can avoid some pointers and memory leaks related bugs (this kind of bug is usually very covert).
Garbage collection is actually transferring the responsibility of the programmer to the computer.
3 things the GC needs to do:
Which memory needs to be recycled
When is it recycled?
How to Recycle
1 Reclaim those objects?
In Java, the Accessibility analysis algorithm is used to determine whether an object is alive or not and whether it can be recycled.
This algorithm uses a series of objects called "GC root" as the root node,
From their start down search, the search traversed the path known as the reference chain (Reference Chain).
When an object does not have a reference chain connected to the GC Root,
That is, from GC Root to this object is unreachable, indicating that this object is not available.
Shown
Object5 OBJECT6 Object7 Although interconnected
But they're not up to the GC root.
So they're going to be judged as objects that can be recycled.
so one of the important questions is, what's GC Root?
In the Java language, it can be considered as GC root:
Variables referenced in the virtual machine stack (can be understood as local variables in the method)
Object referenced by class static property in the method area
The object referenced by a constant in the method area
The object referenced by the JNI (native method) in the local method stack
2 garbage collection algorithm
2.1 mark-Clear algorithm
As the name implies, the method is divided into 2 procedures for marking and clearing
Tag: Mark all object areas that need to be recycled
Clear: Clears all objects in the marked area
Disadvantages of the algorithm:
Efficiency issues: The efficiency of labeling and purging is not high
Space problem: The space generated after the purge is a discontinuous fragment
Cannot meet the needs of large objects in subsequent runs
2.2 Copy Algorithm
Divides the entire space into 2 equal areas, using only one of the areas at a time
When a piece of memory is not enough, copy the live object to another piece of memory
The first chunk of memory is then recycled.
So that every time the entire half of the recovery, there will be no memory fragmentation, the implementation of simple, efficient operation
Problem: The cost of the algorithm is to reduce the memory size to half the original
Workaround: Virtual machines are now replicated in commercial use.
But since all the objects are going to die, they don't divide the memory by a 1:1 ratio.
Instead, divide the memory into a larger Eden area (the new object is the space allocated here)
and 2 blocks of Survivor area. Use Eden and a piece of survivor each time.
When recycled, assigns objects that are still alive in the Eden and survivor areas to another survivor
Finally, clean up the previously used Eden and survivor areas.
These 3 regions are also known as Cenozoic. The default Cenozoic regions of the hotspot are as follows:
each new generation of space available for the entire Cenozoic 90%, namely 80% Eden + 10% 1 survivor
At this time only 10% 1 survivor will be ' wasted '
If another piece of survivor is not stored in the Eden and survivor areas, the surviving objects
You have to rely on other areas to store that old age.
2.3 Labeling-Sorting algorithm
Similar to the tag-purge algorithm, mark all areas that can be reclaimed, and then do not recycle directly,
Instead, move all of the surviving objects to one end, and then clean out the area outside the end boundary directly.
Tag-collation algorithms and marker-purge algorithms are often used in old-age recycling
The objects stored in the old age have been alive for a long time.
and the frequency of garbage collection is not as frequent as new generation
Java GC garbage Collection Algorithm memory allocation