Common GC algorithms, common garbage collectors

tags: jvm  java  

How does the JVM determine that the object can be recycled?

1. The object is not referenced
2. An uncaught exception occurred in the scope
3. The program is executed normally in the scope
4. The program executed System.exit()
5. The program terminates unexpectedly (killed process, etc.)

How does the JVM determine whether it is garbage?

Two algorithms:Reference counting method, reachability algorithm

  1. Reference counting algorithm: In fact, it saves the number of times the object is referenced by allocating a space in the object header. If the object is referenced by other objects, its reference count is increased by one, if the reference to the object is deleted, then its reference count is decreased by one, when the reference count of the object is 0, then the object will be recycled .

There are disadvantages
There is a circular reference problem.

  1. Reachability algorithm: Start the search from GC Root as the starting point, then the objects in the entire connected graph are alive, and objects that cannot be reached by GC Root are garbage objects, which can be recycled by GC at any time.

How does JVM clean up garbage?

Four algorithms:Mark removal algorithmCopy algorithmTag sorting algorithmGenerational collection algorithm
The JVM uses:Generational collection algorithm
3. Mark removal algorithm: "Mark-Remove" algorithm is the most basic algorithm. It is divided into two stages: mark and clear: first mark all objects that need to be recycled, and uniformly recycle all marked objects after marking is completed.

There are disadvantages
Efficiency issues: The marking and removal processes are not efficient.
Space problem: After the mark is cleared, a large number of discontinuous memory fragments will be generated. Too much space fragmentation may cause the program to be unable to find enough continuous memory and have to trigger another garbage in advance when the program needs to allocate large objects in the future operation. Collect actions.

  1. Copy algorithm: In order to solve the serious defect of the memory fragmentation of the mark removal algorithm, a copy algorithm is proposed. The main idea of ​​the copy algorithm is to divide the memory into two areas of equal size according to the memory capacity. Only one of them is used each time. When this memory is full, the surviving objects are copied to another memory, and the garbage objects in the memory are cleaned up.

There are disadvantages
Although the replication algorithm is simple to implement, has high memory efficiency, and is not prone to fragmentation, the biggest problem is that the available memory is compressed to half of the original. And if the number of surviving objects increases, the efficiency of the Copying algorithm will be greatly reduced.

  1. Tag sorting algorithm: Combines the above two algorithms and proposes to avoid defects. The marking phase is the same as the marking cleanup algorithm. After marking, the objects are not cleaned up, but the live objects are moved to one end of the memory. Then clear the objects outside the end boundary.

  2. Generational collection algorithm: Combining the comprehensive analysis of the above three algorithms and the characteristics of the life cycle of JVM memory objects, a new garbage collection algorithm-the generational collection algorithm was born. The core idea is to divide the memory into different domains according to the different life cycles of the object's survival. In general, the GC heap is divided into the Tenured/Old Generation and the Young Generation. The characteristic of the old generation is that only a small number of objects need to be collected during each garbage collection, and the characteristic of the new generation is that a large amount of garbage needs to be collected during each garbage collection, so different algorithms can be selected according to different regions.
    The new generation-replication algorithm: Because most of the objects in the new generation are collected every time garbage collection, that is, there are fewer operations to copy, but the new generation is usually not divided according to 1:1. Generally, the new generation is divided into a larger Eden space and two smaller Survivor spaces (From Space/S1, To Space/S2). Each time the Eden space and one of the Survivor spaces are used, when reclaiming, the The surviving objects in the two spaces are copied to the other Survivor space.
    Old age-mark copy algorithm:

    1. The memory allocation of the object is mainly in the Eden Space of the new generation and the From Space of the Survivor Space (the piece where Survivor currently stores the object). In a few cases, it will be directly allocated to the old generation;
    2. When the space of the new generation Eden Space and From Space is insufficient, a GC will occur. After the GC, the surviving objects in the EdenSpace and From Space areas will be moved to To Space, and then Eden Space and FromSpace will be cleaned up;
    3. If To Space cannot store an object enough, store the object to the old generation;
    4. After GC, Eden Space and To Space are used, and the cycle is repeated;
    5. When the subject avoids a GC in the Survivor area, its age will be +1. By default, objects whose age reaches 15 will be moved to the old age;

JVM garbage collector type

The 3 groups are:Serial and Serial Old, PS and PO, ParNew and CMS

STW: Stop temporarily until the thread is cleaned up before continuing.

Serial and Serial Old operating modes: temporarily stop and waitSingle threadIf the cleaning is over and then continue, there will be a stuttering phenomenon.

PS and PO operating modes: temporarily stop and waitMultithreadingIf the cleaning is over and then continue, there will be a stuttering phenomenon.

jdk1.7/1.8 default garbage collectorPS and POCombination, how to query the type of garbage collector:

  1. Command under Window:java -XX:+PrintCommandLineFlags -version
  2. Virtual machine (Centos): Need to use tools or log analysis*;

What is CMS?

The full name of CMS: concurrent mark sweep, its meaning is to solveSTW stuttering phenomenon, ParNew is specifically designed to work with CMS, ParNew is approximately equal to PS, but the only difference is that it has enhanced modules for PS.

Several stages of CMS work:

  1. initial mark (initial mark) pause time: about 200ms; make comparison: G1: about 10ms, ZGC: about 1ms
  2. concurrent mark (concurrent mark)
  3. remark
  4. concurrent sweep (concurrent sweep) garbage appears again: floating garbage

There are disadvantages
Fragmentation of memory; if the execution of the object from the young generation to the old generation fails, Serial Old will be executed again for defragmentation. If floating garbage is generated, it can only wait for the next GC collection process.

Intelligent Recommendation

Understanding of several common garbage collectors

Briefly talk about the next several garbage collectors, which contain a lot of my personal understanding, it is inevitable that there are errors and omissions, please correct me Borrow a picture New g...

Introduction to ten common garbage collectors

Ten common garbage collectors: Serial, SerialOld, ParallelScavenger, ParallelOld, ParNew, CMS, G1, ZGC, Shenandoah, Epsilon. Serial, SerialOld, ParallelScavenger, ParNew, CMS are all physically and lo...

Summary of common garbage collectors in JVM

            Evaluate GC performance indicators // Mainly based on throughput and pause time evaluation Throughput: the percentage of time spent running user code in total...

Some common garbage collectors in java

Garbage collector Overview Rubbish: There is no object pointed to by any pointer in the running program, and this object is garbage that needs to be recycled. Garbage collection period: The garbage co...

What are the common garbage collectors of Java?

What are the common garbage collectors of Java? The garbage collection mechanism is Java's signature ability, which greatly improves development efficiency. Nowadays, garbage collection has almost bec...

More Recommendation

Several garbage collectors for GC

introduction:If the collection algorithm is a method of memory recycling, the garbage collector is the concrete implementation of memory recycling.   Serial collector The serial collector is the ...

Common GC algorithms in Java_garbage collector and memory allocation_G1 garbage collector

Common GC algorithms Reference counting: Every object has a counter, the object is referenced once, the counter is +1, when the object reference fails once. The counter -1, when the object counter is ...

Garbage collection mechanism and common GC algorithms in Java virtual machines

1. Common GC algorithms 1. Mark-clearing algorithm Mark the objects that need to be recycled and then clean them directly in GC. Disadvantages: It will cause a large number of discontinuous memory fra...

GC common algorithms

1.GC (Garbage Collection) java garbage collection is an automatic process to manage the runtime memory used by programs.by doing it automatic JVM relieves the programer of the overhead of assigning an...

Introduction to common GC algorithms

Four GC algorithms Overview Reference counting Copying algorithm (Copying) Mark-Sweep Mark compression algorithm (Mark-Compact) to sum up Overview When JVM is performing GC,The above three memory area...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top