Home  >  Article  >  Web Front-end  >  Java Virtual Machine Learning - Garbage Collection Algorithm

Java Virtual Machine Learning - Garbage Collection Algorithm

黄舟
黄舟Original
2017-02-17 10:45:271560browse

Tracking collector

The tracing collector adopts a centralized management method to globally record the reference status between objects. Starting from a series of GC Roots objects, search all reference chains downward from these nodes. When an object does not have any reference chain to GC Roots, it proves that this object is unavailable.

In the figure below, although objects Object6, Object7, and Object8 refer to each other, their GC Roots are unreachable, so they will be judged as recyclable objects.


#Objects that can be used as GC Roots include:

Virtual machine A reference object in the stack (local variable table in the stack frame).

Object referenced by the class static property in the method area

The object referenced by the constant in the method area

The JNI reference object in the local method stack.


There are Three implementation algorithms: copy, mark removal, and mark compression.

1. Mark-clear algorithm

The mark and clear algorithm is the most basic collection algorithm, and other collection algorithms are based on this idea. The mark and clear algorithm is divided into two stages: "marking" and "clearing": first, mark the objects that need to be recycled, and then clear the objects uniformly after the marking is completed.

Its main disadvantages:

①. The marking and clearing process is not efficient

##②. After the marking is cleared A large number of discontinuous memory fragments will be generated.


2. Replication Algorithm

It divides the available memory capacity into two equal-sized blocks, and only uses one of them at a time. When this block is used up, copy the surviving objects to another block, and then clean up the used memory space at once. In this way, one piece of memory is recycled every time, and fragmentation will not occur. Just move the pointer of the heap and allocate memory in order. It is simple to implement and efficient to run.

##Main Disadvantages:

##The memory is reduced to half of the original size.

##                        


##3. Marking

- Sorting algorithm

The mark operation is consistent with the "mark-clear" algorithm. Subsequent operations not only clean up the object directly, but also clean up the useless objects. After the object is completed, move all surviving objects to one end and update the pointers referencing their objects.

##Main Disadvantages:

On the basis of mark-clearance, objects need to be moved. The cost is relatively high, but the advantage is that there will be no memory fragmentation.

#

Reference counting collector

The reference counting collector uses decentralized management method, recording whether the object is referenced through a counter. When the counter reaches 0, it means that the object is no longer in use and can be recycled.

Main disadvantages:

In the scenario of circular references Recycling cannot be achieved. For example, in the figure below, ObjectC and ObjectB refer to each other, so even if ObjectA releases its references to ObjectC and ObjectB, it cannot be recycled. sunJDK does not use this method when implementing GC.



# The above is the content of Java virtual machine learning - garbage collection algorithm. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn