Before the garbage collector recycles the heap, the first thing to do is to determine which of these objects is "alive" and which ones are "dead" (that is, objects that cannot be used in any way).
First, the reference counting algorithm
Add a reference counter to each object, and whenever there is a reference to it, the counter value is plus l; When the reference fails, the counter value is reduced by L: Any object with counter 0 at any moment is impossible to be used again. The reference counting algorithm (Reference counting) is simple and efficient, and in most cases it is a good algorithm. There is no reference counting algorithm in the Java virtual machine to manage memory, the main reason is that it is difficult to solve the problem of circular reference between objects.
Two, accessibility analysis algorithm
is through a series of objects called "GC Roots" as the starting point, starting from these nodes to search down, the path of the search is called the reference chain (Reference Chain) , when an object to the GC Roots no reference chain connected (in Tuke words, IS from GC Roots to this object is not available, it proves that this object is not available.
the objects that can be used as GC Roots include the following categories:
-
-
The object referenced in the virtual machine money (local variable table in the stack frame).
-
The image referenced by the class static property in the method area.
-
The object referenced by the constant in the method area ...
-
A reference to the JNI (native method) in the local method warfare.
Third, another reference
After JDK 1.2, Java expanded the concept of references into strong references (strong Reference), soft references (Soft Reference), weak references (Weak Reference), virtual references (Phantom Refe rence), their reference strength is gradually reduced in turn.
-
-
Strong references are common in program code. Similar to Object obj = new Object (), as long as a strong reference exists, the garbage collector never reclaims objects that are referenced.
-
Soft references are used to describe objects that are useful but not necessary. The object that is associated with the soft reference. Before the system will occur with a memory overflow exception. These objects will be listed in the garbage collection scope for a second collection. If this collection still does not have enough memory, it will throw out the memory mi exception. After JDK1.2, the soft Reference class is provided to implement the soft reference.
-
Weak references are also used to describe non-required objects. Objects that are associated with a weak reference can only survive until the next garbage collection occurs. When the garbage collector works on the elbow, the object associated with the weak reference is reclaimed regardless of whether the current memory is sufficient. After JDK 1.2, the WeakReference class was provided to implement weak references.
-
A virtual reference , also known as a phantom reference or phantom Reference, is the weakest reference relationship. Whether an object has a virtual reference exists, does not have an impact on its lifetime, nor does it have a virtual reference to obtain an object instance. The only purpose of setting a virtual reference association for an object is to expect to receive a system notification when the object is reclaimed by the collector. After JDK 1.2, the Phantomreference class is provided to implement the virtual reference.
Third, survival or death
Even in the accessibility analysis algorithm of the non-edge of the object, is not "immortal", to truly declare an object to die, at least two times the threat of the marking process: If an object is found to have no reference chain connected to the GC Roots after the accessibility analysis, it will be marked for the first time and filtered. The criteria for filtering is whether this object is necessary to perform a finalize () method. When the object does not overwrite the Fnalize () method, or the Finalize () method has been called by the virtual machine, the virtual machine treats both cases as "not required, to be executed."
If the object is judged to be necessary to execute the Finalize () method, then the object will be placed in a queue named F-queue, which is then executed by a low-priority finalizer thread that is automatically created by the virtual machine. " Execute " means that the virtual opportunity triggers this method, but does not promise to wait for it to end. Finalize () the more the object escapes the last chance of death, the GC will then make a second small-scale mark on the objects in the F-queue, and if the object is not escaped at this time, it can be recycled.
Three, method recovery area
method Area (HotSpot virtual machine permanent generation) garbage collection mainly recycles two parts: obsolete constants and useless classes. Classes need to meet the following 3 conditions to be considered "useless classes":
-
-
All instances of the class have been reclaimed, that is, no instances of the class exist in the Java heap.
-
The ClassLoader that loaded the class have been recycled.
-
The corresponding Java.lang.Class object of this class is not referenced anywhere, and cannot be accessed from anywhere by reflection on the method of the class
Virtual machines can reclaim useless classes that meet the above 3 criteria, but do not necessarily recycle them.
"See" "Understanding Java Virtual Machine (second edition)" "Zhou Zhiming"
Blog: http://blog.csdn.net/derrantcm/article/details/44409835
Java Virtual machine Learning (iii)