Analysis of Java garbage collection mechanism

In C + +, the memory occupied by objects will be occupied until the program ends running, and cannot be allocated to other objects until it is explicitly released; In Java, when no object reference points to the memory originally allocated to an object, the memory becomes garbage, and a system level thread of the JVM will automatically release the memory block.

Advantages of garbage collection:

1. Garbage collection can automatically release memory space and reduce the burden of programming, so that Java programmers do not need to consider memory management when writing programs.

2. The garbage collection mechanism makes the objects in Java no longer have the concept of "scope", and only the referenced objects have "scope".

3. Garbage collection mechanism prevents memory leakage and can make effective use of free memory. At the same time, since the creation of objects and the garbage collector release the memory space occupied by discarded objects, memory will be fragmented (the free memory hole between the memory blocks allocated to objects). Garbage collection can remove these memory record fragments, that is, move the occupied heap memory to one end of the heap, so that the JVM can allocate the cleaned up memory to new objects.

Disadvantages of garbage collection:

1. The overhead of garbage collection mechanism affects program performance. The Java virtual machine must track the useful objects in the running program and finally release the useless objects, which takes processor time.

2. Incompleteness of garbage collection algorithm. Some previous garbage collection algorithms can not guarantee 100% collection of all waste memory. Of course, with the continuous improvement of garbage collection algorithm and the continuous improvement of software and hardware operation efficiency, these problems can be solved.

Common algorithms for garbage determination:

1. Reference counting collector

Implementation principle: add a reference counter to the object. Whenever the object is referenced in a place, the counter value is + 1; When the reference expires, the counter value is - 1. An object with a count value of 0 at any time can no longer be used. This method is characterized by simple implementation and high efficiency, but it can not solve the problem of circular reference. Therefore, this method is not adopted in Java (Python adopts reference counting method). The specific code is shown in the following figure:

public class Main {
    public static void main(String[] args) {
        MyObject object1 = new MyObject();
        MyObject object2 = new MyObject();

        object1.object = object2;
        object2.object = object1;

        object1 = null;
        object2 = null;
    }
}

class MyObject{
    public Object object = null;
}

After assigning object1 and object2 null, the objects pointed to by object1 and object2 can no longer be accessed. However, because they refer to each other, their reference count is not 0, so the garbage collector will never recycle them.

Therefore, there is no such scheme in Java to determine the "viability" of objects.

2. Tracing algorithm (tracing collector)

Implementation principle: Imagine all referenced objects as a tree. Start from the root node GC roots of the tree and continue to traverse to find all connected branch objects. These objects are called "reachable" objects or "living" objects. Other objects are regarded as "unreachable" objects of "death", or "garbage".

In the following figure, object5, object6 and object7 are unreachable objects, which are considered "dead" and should be recycled by the garbage collector.

In the Java language, objects that can be used as gcroots include the following:

(1). The object referenced in the virtual machine stack (the local variable area in the stack frame, also known as the local variable table).

(2). An object referenced by a class static property in the method area.

(3). The object referenced by the constant in the method area.

(4). The object referenced by JNI (native method) in the local method stack.

Garbage collection algorithm:

Since the Java virtual machine specification does not make clear provisions on how to realize garbage collection, virtual machines of various manufacturers can adopt different ways to realize garbage collection, so only several common garbage collection algorithms are listed.

1. Mark sweep algorithm

The mark clear algorithm is divided into two stages. In the first stage, all objects that need to be recycled are marked, and in the second stage, the space occupied by the marked objects is recycled. This method is simple and convenient, but it is prone to memory fragmentation.

2. Copying algorithm

The algorithm is simple and efficient, and will not produce memory fragments, but the available memory is reduced to half of the original. If there are many surviving objects, the efficiency will be greatly reduced. Therefore, the algorithm is suitable for the situation of less surviving objects and more garbage.

3. Mark compact algorithm

Implementation principle: the marking stage of the algorithm is consistent with the mark sweep algorithm, but the second stage is not to clear directly, but to move all living objects to one end, and then clean up the memory outside the end boundary.

4. Generational collection algorithm

Implementation principle: according to the life cycle of the object, the memory is divided into different regions, generally divided into the aged generation and the young generation. The most suitable collection algorithm can be adopted according to the characteristics of different generations.

Old age: store objects that have survived for a period of time. These objects were created early and have survived. These objects are stored in the old age area. During each garbage collection, only a small number of objects need to be recycled. Generally, the mark compact algorithm is used.

New generation: it stores newly created objects. New objects will be created continuously when the code is running, and these newly created objects will be placed in this area. During each garbage collection, a large number of objects need to be recycled. Generally, the copying algorithm is used because most objects need to be recycled every garbage collection in the new generation, that is, there are fewer operations to copy.

The copying algorithm divides the memory equally into two parts, which is actually problematic. For the new generation, new objects are created continuously. If only half of the available memory is available, it is obvious that garbage collection should be carried out continuously, which will affect the operation of normal programs.

Therefore, in fact, we divide it according to the ratio of 8:1:1, and call it Eden, survivor A and survivor B in turn. Eden means the garden of Eden, which describes that many new objects are created in it; The survivor area is the survivor, that is, the object that still survives after garbage collection.

working principle

1. First, the Eden area is the largest, providing external heap memory. When the Eden area is almost full, perform minor GC (reclaiming memory from the younger generation space is called minor GC), put the surviving objects into survivor a area, empty the Eden area, and make the Eden area continue to provide heap memory to the outside;

2. When Eden area is filled again, miner GC is performed for Eden area and survivor a area at the same time, put the living objects into survivor B area, and empty Eden area and survivor a area at the same time;

3. The Eden area continues to provide external heap memory and repeats the above process, that is, after the Eden area is filled, the living objects of the Eden area and one survivor area are placed in another survivor area;

4. When a survivor area is filled, and there are still objects that have not been copied, or some objects are repeatedly survived for about 15 times, put these remaining objects in the elderly generation area;

5. When the older generation area is also filled, perform major GC (reclaiming memory from the older generation space is called major GC).

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>