JVM Tuning Summary (3) - Basic Garbage Collection Algorithms

 

Garbage collection algorithms can be divided from different perspectives:

According to the basic recycling strategy

Reference Counting:

Older recycling algorithm. The principle is that this object has a reference, that is, a count is increased, and a reference is deleted by a count. During garbage collection, only objects with a count of 0 are collected. The most fatal thing about this algorithm is that it cannot handle the problem of circular references.

 

Mark-Sweep:

 

 

This algorithm executes in two stages. The first stage marks all referenced objects starting from the reference root node, and the second stage traverses the entire heap and clears unmarked objects. This algorithm needs to suspend the entire application, and at the same time, it will generate memory fragmentation.

 

Copying:

 

 

This algorithm divides the memory space into two equal regions, only one of which is used at a time. During garbage collection, it traverses the currently used area and copies the objects in use to another area. The sub-algorithm only processes the objects in use each time, so the cost of copying is relatively small, and at the same time, the corresponding memory can be sorted after copying, and there will be no "fragmentation" problem. Of course, the disadvantage of this algorithm is also obvious, that is, it requires twice the memory space.

 

Mark-Compact:

 

 

This algorithm combines the advantages of both "mark-and-clean" and "copy" algorithms. It is also divided into two stages. The first stage starts from the root node to mark all referenced objects, and the second stage traverses the entire heap, clears unmarked objects and "compresses" the surviving objects into one of the heap, and arranges them in order. This algorithm avoids the "mark-and-sweep" fragmentation problem, and also avoids the space problem of the "copy" algorithm.

Treated by partition

Incremental Collection: A real-time garbage collection algorithm, that is, garbage collection is performed while the application is in progress. I don't know why the collector in JDK5.0 does not use this algorithm.

 

Generational Collecting: A garbage collection algorithm based on an object life cycle analysis. Divide objects into young generation, old generation, and persistent generation, and use different algorithms (one of the above methods) to recycle objects with different life cycles. The current garbage collector (starting from J2SE1.2) uses this algorithm.

 

By system thread

Serial collection: Serial collection uses a single thread to handle all garbage collection work, because it does not require multi-threaded interaction, it is easy to implement, and it is relatively efficient. However, its limitation is also obvious, that is, it cannot use the advantages of multiple processors, so this collection is suitable for uniprocessor machines. Of course, this collector can also be used on multiprocessor machines with small data volume (about 100M).

 

Parallel collection: Parallel collection uses multiple threads to handle garbage collection work, so it is fast and efficient. And theoretically, the more the number of CPUs, the more the advantages of the parallel collector can be reflected.

 

Concurrent collection: Compared with serial collection and parallel collection, the first two need to suspend the entire operating environment when performing garbage collection work, while only the garbage collector is running. Therefore, the system will have a significant pause during garbage collection. , and the pause time will be longer as the heap is larger.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324773907&siteId=291194637