JVM garbage collection algorithm: 1. "Mark-Clear" algorithm; first mark all objects that need to be recycled, and then uniformly recycle all marked objects after the marking is completed. 2. Copy algorithm; divide the memory into two equal-sized blocks, and only use one of them at a time. 3. "Mark-Organization" algorithm; 4. Generational collection algorithm.
The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.
Two concepts:
New generation: An area where objects with short life cycles are stored.
Old generation: an area where objects with long life cycles are stored.
Same points: They are all on the Java heap
1. Mark-clear algorithm
Execution steps:
Illustration:
##Disadvantages:
2. Copy algorithm
Divide the memory into two equal-sized blocks, and only use one of them each time piece. When a block is used up and GC is triggered, the surviving objects in the block are copied to another area, and then the useless memory is cleared at once. The next time the GC is triggered, the surviving parts of that block will be copied to this block, and then that block will be erased, and the cycle repeats.
Illustration:
##AdvantagesRelative to the mark-clean algorithm, it solves the problem of memory fragmentation.
The memory utilization is not high, only half of the memory can be used at a time.
Research shows that most of the objects in the new generation are "live and die", that is, the life cycle is very short and the longer the object lives, the The harder it is to recycle. When GC occurs, there are many objects that need to be recycled and very few survive. Therefore, there are very few objects that need to be moved to another memory, so there is no need to divide the memory space 1:1. Instead, the entire new generation is divided into three areas in a ratio of 8:1:1. The largest area is called the Eden area, and the two smaller areas are called To Survivor and From Survivor respectively.
During the first GC, only the surviving objects of Eden need to be copied to To. Then the entire Eden area is recycled. When GC occurs again, copy the surviving Eden and To to From, and repeat this process. In this way, the memory available in each new generation accounts for 90% of the entire new generation, greatly improving memory utilization. [Related recommendations:
Java Video Tutorial]But there is no guarantee that the surviving objects each time will always be less than 10% of the entire new generation. At this time, the copied past cannot be saved, so here Another block of memory, called the old generation, will be used to perform allocation guarantees and store objects in the old generation. If it is not enough, OOM will be thrown.
Old generation: stores objects in the new generation that have survived multiple recycling attempts (default 15 times).
3. Mark – Collation Algorithm
Because the previous copy algorithm keeps copying when the survival rate of the object is relatively high. Coming over and copying the past is meaningless and a waste of time. Therefore, a "marking sorting" algorithm was proposed for the old age.Execution steps:
Marking: Mark those that need to be recycled
##4. Generational collection algorithm
Most commercial virtual machines currently use this generational collection algorithm. This algorithm has no new content. It just divides the memory into the new generation and the old generation based on the survival time of the object. This way Corresponding algorithms can be adopted for different areas. For example:
The difference between MinorGC and FullGC
MinorGC: Garbage collection that occurs in the new generation. Because of the characteristics of the new generation, MinorGC is very Frequent, the recycling speed is relatively fast, and the amount recycled each time is also large.
FullGC: Garbage collection that occurs in the old generation, also called MajorGC, is relatively slow, about 10 times slower than MinorGc. A FullGC is usually accompanied by multiple MinorGCs.
For more programming related knowledge, please visit:Programming Video! !
The above is the detailed content of What are the jvm garbage collection algorithms?. For more information, please follow other related articles on the PHP Chinese website!