The JVM manages the heap memory, almost holds all the object instances, if an object is already used (no reference to it), then the memory occupied by the object should be recycled, which is the work of the JVM garbage collector
Before the garbage collector recycles the heap memory, the first thing to do is to determine which objects are "alive", which are "dead", whose memory is going to be recycled, and--the root search algorithm
Before we talk about the root search algorithm, because an object does not have any references to it, the memory is recycled, so let's start by talking about what the reference
Traditional reference: If the data stored in the reference type represents the starting address of another piece of memory, it is said that this memory represents a reference
New concepts cited: References are divided into strong references, soft references, weak references, and virtual references, and these four reference intensities gradually weaken in turn
1, strong reference: Strong reference refers to the program code is universal, similar to object A = new object () such a reference, as long as the reference is still present, the garbage collector will never reclaim the memory of the Reference object
2. Soft references: Soft references are used to describe some objects that are also useful, but are not required. For objects associated with soft references, these objects will be included in the collection and reclaimed for a second time, before memory overflow exceptions occur.
3. Weak references: Used to describe non-required objects, the strength is weaker than soft references, the objects associated with the weak reference can only survive until the next garbage collection occurs, and when the garbage collector is working, it is recycled, regardless of the current memory.
4. Virtual reference: It is the weakest reference relationship. Whether an object has a virtual reference exists, does not have an impact on its lifetime, and cannot obtain an object instance through a virtual reference. Its sole purpose is to be able to receive a system notification when the object is reclaimed by the collector.
So let's talk about a root search algorithm that determines if an object has a condition to be recycled.
The basic idea of this algorithm is to use a series of objects called "GC Roots" as the starting point, starting from these nodes to search down, search through the path is the reference chain, when an object to the GC Roots no reference chain connected, it proves that this object is not available, unreachable, should be recycled. Shown
In the Java language, the objects that can be used as GC roots include the following:
The referenced object in the virtual machine stack (the local variable table in the stack frame).
The object referenced by the class static property in the method area.
The object referenced by a constant in the method area.
The object referenced by the JNI (that is, the generally said native method) in the local method stack.
How does a Java virtual machine determine that an object is dead? (which memory needs to be recycled)