This article has participated in the third “topic writing” track of the Denver Creators Training Camp. For details, check out: Digg Project | Creators Training Camp third is ongoing, “write” to make a personal impact

The garbage collection

Garbage collection in Java refers to the cleaning and recycling of objects in the memory heap that have died or have not been used for a long time. Memory in most of the objects are created with method of execution, after the method performs these objects will not be used again, but the resulting object will not be removed, so our memory inside the object will be more and more, then we will need a mechanism to get rid of this object will not be used again, Objects that will not be used again are called garbage.

Determination of garbage objects

The JVM’s GC work is mainly aimed at the heap memory. Before GC work, it is necessary to determine whether the object instances in the heap memory are garbage. The following two algorithms are usually used: reference counting method and reachability analysis method.

Reference counting method

Reference counting method is to add a reference counter object, and then use a additional memory area to store the number of each object is referenced, when objects each have a place to refer to it, that we in the object reference count will add 1, and each has a reference fails, we can effectively reduce the object reference count will be 1, when the object is cited to 0, Then we can assume that the object will not be used again, in this way we can quickly and intuitively locate the recyclable objects, so as to clean up.

Disadvantages of reference counting

1. Unable to solve the problem of circular references

When there are circular references to objects, such as object A referring to B and object B referring to A, and neither of them is referred to by any other object, then this part of the object is also “recyclable”, but reference counting cannot locate it.

2. Another aspect of reference counting is that it requires extra space to keep track of how many times each object is referenced, which also requires extra maintenance.

Accessibility analysis

Object using a series of GC Roots (including: virtual machine stack object references, method of area in class static attribute reference object, method a const reference on the object, the method of local stack JNI references of objects) as a starting point, search downward from node, when not being GCRoots link to the object can be recycled, the following figure

Garbage collection algorithm

After the reachability analysis method is used to locate whether the object is alive or not, it is necessary to clean up the dead object through some strategy, and then tidy up the surviving object. This process involves three algorithms, namely, mark clearing method, mark copying method and mark sorting method.

Mark-sweep

The JVM scans all object instances and marks active objects using a root search algorithm. The JVM scans all objects again and removes unmarked objects with no action

The pros and cons of marking cleanup

Mark cleanup is characterized by its simplicity, directness and speed, especially suitable for scenarios where there are few recoverable objects. The disadvantages are: 1, will cause discontinuous memory space, namely space fragmentation; 2. Unstable performance: the objects in memory need to be recycled. When a large number of objects in memory need to be recycled, usually these objects may be scattered, so the process of cleaning will be time-consuming, and the speed of cleaning will be slower.

Mark copy

First, it divides the memory into three areas, one for newly created objects called Eden and the other two for surviving objects called S1 and S2. There will be two situations during reclamation. One is to copy the viable objects in Eden and S1 to S2, and the other is to copy the viable objects in Eden and S2 to S1. That is to say, S1 and S2 will only be used in one area at the same time. In this way, there is always a blank q area for the next GC to hold the live objects, and the original area does not need to consider keeping the live objects, so you can clean all the objects at once, which is simple and straightforward but also ensures the memory continuity of the cleared memory area. The JVM scans all objects, marks the referenced objects with the root search algorithm, and then applies for a new memory space. The marked objects are copied to the new memory space. When the surviving objects are copied, the original memory space is emptied and the new memory is used as the OBJECT storage space for the JVM. In this way, the memory fragmentation problem is solved, but if there are many objects, the re-application of memory space will be large, and in the case of insufficient memory, it will have a great impact on the JVM operation

Advantages and disadvantages of tagging copying

Advantages: copy the method to solve the marking QingChuFa problem of space debris, and with the method of mobile object survival, every time clean up against is a block of memory, so clear recyclable object has a high efficiency, but because of moving objects so will spend some time here, so copy method and efficiency will be lower than marking QingChuFa.

Deficiencies: 1. It will waste some space: It is not difficult to find from the above figure that there is always a free memory area that cannot be utilized, which also causes a waste of resources. 2, live objects will be very time-consuming: because the process of copying moving objects is time-consuming, this is suitable for not only need to move the object itself but also need to modify the reference address of these objects, so when there are many live objects will be very time-consuming.

Label finishing

It is designed specifically for garbage collection when there are many living objects, while avoiding space debris of course. The marking method is divided into two stages: marking and sorting. In the marking stage, the surviving objects and recoverable objects will be marked out first. This phase moves the living object to one end of memory and then clears the objects outside the living object boundary.

Advantages and disadvantages of mark finishing method:

Advantages: It solves the space fragmentation problem of tag cleanup, and it does not require free memory space like tag copy, so it is very suitable for scenarios with many viable objects.

Inadequate: tag finishing method is three garbage collection algorithm performance in the lowest one, because the tag finishing method need not only when moving objects moving objects, and additional maintenance object reference address, this process may be to memory after several times of scanning positioning to complete, do the more the more time you must use.

Generational recycling

Generation collection algorithm is a combination of the above three basic algorithm ideas and different algorithms for different situations. According to the different life cycle of objects, the memory is divided into several blocks. In the new generation, a large number of objects are found dead and only a few survive in garbage collection, so the replication algorithm is selected, and only a small amount of the replication cost of the surviving objects can be collected. In the old days, because the object had a high survival rate and there was no extra space to allocate it, it had to be recycled using mark-clean or mark-clean algorithms.