【JVM】Garbage collection algorithm
Java is a high-level language, it does not need to be manually opened up by the programmer like the C language, manually free the memory after use, Java has its own garbage collector, for those objects in memory that are not used, the garbage collector will automatically collect, garbage collection uses different algorithms, different algorithms for different scenarios, in order to improve the effect of recycling efficiency.
There are three main garbage collection algorithms in the JVM, tag cleaning, tag grooming, and copy algorithms.
Mark Sweep
Marker purging is divided into two phases, marking and purging.
Let's assume that the current memory allocation in memory is as follows:
You can see that the green and red objects should not be recycled due to the GC Root association, and they should not be recycled through reachability analysis, but the blue objects object4 and object6 should be recycled.
Marking stage: The object relationship will be traversed from the root node, and the visited object will be marked as non-recyclable, that is, the object is reachable.
Purge phase: Unmarked objects are cleaned up and memory is returned so that the returned space can be reused.
After the recycling, the memory situation will become as shown in the following figure:
The middle part of the two red is the part that was released after this GC.
The disadvantage of the tag cleanup algorithm is that it causes memory fragmentation problems.
Because the reclaimed memory is discontinuous, it may cause memory fragmentation problems, which may reduce the memory usage, for example, the reclaimed memory is 10KB, then it means that the maximum can hold 10KB of objects, if you want to put objects larger than 10KB, it is impossible to put in, which will also lead to frequent garbage collection.
Mark Compact
Marking and finishing is also divided into two stages, marking and finishing.
Marking stage: The mark here is the same as the mark in the marker clearing, and it is the object that is marked out to reach.
Grooming phase: The defragmentation phase is actually to solve the fragmentation problem caused by the tag clearing algorithm, because when purging, the available objects are moved to make them compact, so that there is no memory fragmentation.
After the move is complete, it becomes like this
At this point, there is no fragmentation in the memory, and new objects can continue to allocate memory later.
The tag grooming algorithm obviously has no memory fragmentation, but it also has drawbacks, because the grooming is to move the object, the memory address will change, and the variable reference will definitely change. Then this efficiency must be lower.
Copy algorithm (Copy)
The replication algorithm divides the memory into two parts of equal size, one part is called the FROM area and the other part is called the TO area. The replication algorithm is also first of all to mark those surviving reachable objects, and the marking operation is no different from the previous two algorithms. After tagging, copy the marked object to another area, such as the object in the FROM area, copy it to the TO area.
After copying, the FROM area can be cleaned up, and FROM and TO are swapped.
In the replication algorithm, the TO region is always the space of space.
The replication algorithm is also not fragmented, and it also has disadvantages: the memory is divided into two halves, and half is always free, so the memory utilization is first. Free memory is actually quite wasteful.
summary
The three algorithms will be used in different garbage collectors, but different garbage algorithms are used according to different situations, and different garbage collectors choose different algorithms, and also respond to different situations and choose the best.