[Garbage Collection] A daily summary of the GC series

1. What is GC? Why is there GC?
GC means garbage collection. Its full name is garbage colector (cleaner). Memory processing is a place where programmers are prone to problems. Forgetting or wrong memory recycling can cause program or system instability or even crash. GC provided by Java The function can automatically monitor whether the object exceeds the scope to achieve the purpose of automatically reclaiming the memory. The Java language does not provide a display operation method for releasing the allocated memory.
2. What is the JAVA garbage collection mechanism?
In Java, the programmer does not need to explicitly release the memory of an object, but is executed by the virtual machine itself. In the JVM, there is a garbage collection thread, which is of low priority and will not be executed under normal circumstances. It will only be triggered when the virtual machine is idle or the current heap memory is insufficient. Reference objects and add them to the collection to be recycled for recycling.
3. How to judge whether an object is alive? (Judging method of GC object)
There are two ways to judge whether an object is alive:
1. Reference counting method The
so-called reference counting method is to set a reference counter for each object. Whenever there is a place to reference the object, the counter is incremented. One, when the reference becomes invalid, the counter decreases by one. When the reference counter of an object is zero, it means that the object is not referenced, that is, the "dead object", and it will be garbage collected.

One drawback of the reference counting method is that it cannot solve the circular reference problem . That is to say, when object A refers to object B, and object B refers to object A, then the reference counters of A and B objects are not zero at this time, which makes it impossible Complete garbage collection, so mainstream virtual machines do not use this algorithm.
2. Reachability algorithm (reference chain method)
The idea of ​​this algorithm is: start from an object called GC Roots and search downwards. If there is no reference chain connection between an object and GC Roots, then this object is not available .

The objects that can be used as GC Roots in Java are as follows:

• Objects referenced in the virtual machine stack

• Objects referenced by static properties of the method area

• Objects referenced by the method area constant pool

• Objects referenced by the native method stack JNI

Although these algorithms can determine whether an object can be recycled, when the above conditions are met, an object may not necessarily be recycled. When an object is unreachable to the GC Root, the object will not be recovered immediately, but in a reprieve phase. To be truly recovered, it needs to be marked twice

If the object does not have a reference chain with GC Root in the reachability analysis, then it will be marked for the first time and a filter will be performed. The filter condition is whether it is necessary to execute the finalize() method. When the object does not cover the finalize() method or has been called by the virtual machine, then it is considered unnecessary. If it is necessary for the object to execute the finalize() method, then the object will be placed in a queue called F-Queue, and the virtual machine will trigger a Finalize() thread to execute. This thread is of low priority and virtual The machine will not promise to wait for it to finish running. This is because if finalize() is slow or deadlock occurs, it will cause F-Queue to wait for the queue and cause the memory recovery system to crash. The GC will mark the object in the F-Queue for the second time. At this time, the object will be removed from the "Recycling" collection, waiting for recycling.
4. The advantages and principles of garbage collection. And consider the two recycling mechanisms
. A significant feature of the Java language is the introduction of a garbage collection mechanism, which solves the most troublesome memory management problem for C++ programmers. It makes Java programmers no longer need to consider memory management when writing programs. Due to a garbage collection mechanism, objects in Java no longer have the concept of "scope", only references to objects have "scope". Garbage collection can effectively prevent memory leaks and effectively use available memory. The garbage collector usually runs as a separate low-level thread. Under unpredictable circumstances, the dead or unused objects in the memory heap are cleaned and recycled. The programmer cannot call the garbage collector in real time to Object or all objects are garbage collected.

The recycling mechanism includes generational copy garbage collection, marked garbage collection, and incremental garbage collection.

5. What is the basic principle of the garbage collector? Can the garbage collector reclaim memory immediately? Is there any way to proactively notify the virtual machine for garbage collection?
For the GC, when the programmer creates an object, the GC starts to monitor the address, size, and usage of the object. Generally, **GC uses a directed graph to record and manage all objects in the heap. **In this way, determine which objects are "reachable" and which objects are "unreachable". When the GC determines that some objects are "unreachable", the GC is responsible for reclaiming these memory spaces. can. The programmer can manually execute System.gc() to notify the GC to run, but the Java language specification does not guarantee that the GC will be executed.
6. Will there be a memory leak in Java? Please describe briefly.
The so-called memory leak means that an object or variable that is no longer used by the program has been occupied in memory. There is a garbage collection mechanism in Java, which can ensure that when an object is no longer referenced, that is, when the object becomes an orphan, the object will be automatically removed from the memory by the garbage collector. Because Java uses a directed graph for garbage collection management, it can eliminate the problem of reference cycles. For example, there are two objects that refer to each other. As long as they are not reachable to the root process, the GC can also recycle them.

Memory leaks in Java: long-lived objects hold references to short-lived objects and memory leaks are likely to occur, although short-lived objects are no longer needed, but because long-lived objects hold their references It can’t be recycled. This is where the memory leak occurs in Java. In layman’s terms, the programmer may create an object and never use the object in the future, but the object has been referenced, that is, the object is useless but cannot Reclaimed by the garbage collector, this is a memory leak that may occur in Java. For example, in the cache system, we load an object in the cache (for example, in a global map object), and then never use it. This object has been referenced by the cache, but it is no longer used.

To check for memory leaks in Java, you must let the program execute all branch situations until the end of the program, and then see whether an object has been used, and if not, you can determine that the object is a memory leak.

If the method of an instance object of an outer class returns an instance object of an inner class, the inner class object is long-term referenced, even if the outer class instance object is no longer used, but because the inner class persists the outer class instance object, this External class objects will not be garbage collected, which will also cause memory leaks.

Another case of memory leakage: when an object is stored in the HashSet collection, the fields in the object that participate in the calculation of the hash value cannot be modified, otherwise, the modified hash value of the object is originally stored in the HashSet The hash value in the collection is different. In this case, even if the contains method uses the current reference of the object as a parameter to retrieve the object in the HashSet collection, it will return the result that the object cannot be found, which will also As a result, the current object cannot be individually deleted from the HashSet collection, causing a memory leak.
6. What will System.gc() and Runtime.gc() do?
These two methods are used to prompt the JVM to perform garbage collection. However, whether to start immediately or delay garbage collection depends on the JVM.
7. In Java, when can an object be garbage collected?
When an object becomes inaccessible to the application currently using it, the object can be recycled.
8. When is the .finalize() method called? What is the purpose of finalization?
When the garbage collector (garbage colector) decides to recycle an object, it will run the finalize() method of the object. Unfortunately in Java, if the memory is always sufficient, then garbage collection may never proceed, that is to say, filalize () may never be executed, obviously it is unreliable to count on it to do finishing work. So what does finalize() do? Its main purpose is to reclaim the memory applied by special channels. Java programs have a garbage collector, so under normal circumstances, memory problems don't need programmers to worry about. But there is a JNI (Java Native Interface) that calls non-Java programs (C or C++), and the job of finalize() is to reclaim this part of the memory.
9. What is distributed garbage collection (DGC)? How does it work?
DGC is called distributed garbage collection. RMI uses DGC to do automatic garbage collection. Because RMI contains references to remote objects across virtual machines, garbage collection is difficult. DGC uses a reference counting algorithm to provide automatic memory management for remote objects.
10. Will garbage collection occur in the permanent generation of JVM?
Garbage collection does not occur in the permanent generation. If the permanent generation is full or exceeds the critical value, a full garbage collection (Full GC) will be triggered.
11. What are the methods of garbage collection in Java?
Mark-Clear:

This is the most basic of the garbage collection algorithm. According to the name, you can know that its idea is to mark which objects to be recycled, and then collect them uniformly. This method is simple, but there are two main problems:

The efficiency is not high, the efficiency of marking and removal is very low;

A large number of discontinuous memory fragments will be generated, causing the program to trigger a GC action early due to insufficient continuous memory when allocating larger objects in the future.

Copy algorithm:

In order to solve the efficiency problem, the copy algorithm divides the available memory into two equal parts according to the capacity, and then only uses one of them at a time. When one memory is used up, the surviving objects are copied to the second memory, and then Clear the first block of memory at one time, and then copy the objects on the second block to the first block. But in this way, the cost of memory is too high, and general memory is basically wasted every time.

So the algorithm is improved. The memory area is no longer divided according to 1:1, but the memory is divided into 8:1:1 three parts, the larger part of the memory is divided into the Eden area, and the rest are two smaller parts The memory area is called Survior area. The Eden area will be used first every time. If the Eden area is full, the objects will be copied to the second memory area, and then the Eden area will be cleared. If there are too many surviving objects at this time that Survivor is not enough, these objects will be passed through The distribution guarantee mechanism is copied into the old age. (Java heap is divided into new generation and old generation)

Mark-organize:

This algorithm is mainly to solve the problem of mark-sweep and generate a large amount of memory fragmentation; when the object survival rate is high, it also solves the efficiency problem of the replication algorithm. The difference is that when the object is cleared, the recyclable object is now moved to one end, and then the object outside the end boundary is cleared, so that no memory fragmentation will occur.

Generational collection:

Most of the current virtual machine garbage collection uses this method, which divides the heap into the young generation and the old generation according to the life cycle of the object. In the new generation, due to the short lifetime of objects, a large number of objects will die every time they are recycled, so the replication algorithm is used at this time.

Objects in the old age have a higher survival rate, and there is no extra space for allocation guarantees.

Guess you like

Origin blog.csdn.net/weixin_42777004/article/details/108601099