In the previous article, I covered several algorithms for how GC recovers these garbage objects after it confirms the garbage object in the GC mechanism. Now introduce how the GC mechanism generally locates (or is called a tag) out of these junk objects.
let's ask ourselves first, how to judge the description. An object can be recycled.
The answer is simple and straightforward. This object will never be called again, so it can be recycled.
So how can you tell if an object is no longer being called?
The answer is also simple, as long as the other objects no longer hold references to the current object, then they can be retracted.
In combination with the above ideas, it is essential to determine what objects can be marked as garbage objects.
For this reason, the two algorithms that are commonly mentioned for locating garbage objects are as follows:
1. Reachabbility Analysis method of accessibility
2. Reference counting algorithm Reference counting
This article mainly introduces the first algorithm, the second algorithm will be introduced in the next article.
Accessibility analysis algorithm
The general idea of accessibility analysis is to determine which objects must not be recycled, and then use these objects as roots to traverse the references held by those roots one at a time until the entire forest is finished. The objects that are traversed are thought to be reachable, objects that can be referenced, and the objects that remain unmarked can be considered as inaccessible garbage objects after they are tagged.
As shown
Advantages
This method of five-way is the algorithm idea, or the implementation of the algorithm, are very simple. At the same time, the error is very small and can handle many unusual scenarios, including circular references (such as the Yellow memory portion). Since its inception, therefore, most
GC, you can see the shadow of the algorithm.
Disadvantages
1, simple is simple, unfortunately the speed is not fast enough. Each calculation needs to be traversed from the root.
2, for only a few variables need to recycle the scene, the algorithm cost-effective too low.
3, the impact on the main program is relatively large, because the need to start from the root layer to explore, so (anti-theft connection: This article starting from http://www.cnblogs.com/jilodream/) in the process of traversal, the relationship between objects can no longer change, Therefore, the main program needs to be paused (this time period is also often referred to as Stop the world). This is often a very unfriendly situation for some scenarios that require timely response.
So what are the objects that can exist as the root of this algorithm? In other words, what are the objects that are definitely not deleted?
in the JVM, there are several situations:
1, in the virtual machine stack, the local variable table stored in the stack frame variable reference
These references represent the references that are held in the currently executing method. Since space is limited, if you have questions about why objects in a local variable table in a stack frame can be root, you can look at my other article about Virtual machine memory: https://www.cnblogs.com/jilodream/p/6147791.html
2. Objects referenced by static properties in the method area
These references represent references that are held by static variables in the class object. Once a class is loaded, static variables in it are not recycled with the collection of an instance object.
3. Reference information for constants in the method area: information that is referenced by constants in the method area.
4. In the local method stack, the variable that the native method is using: The variable used by the virtual machine local method.
The above objects are not absolute forever and can exist as the root of the algorithm. When a class is unloaded and a constant is recycled, some objects that would otherwise be rooted will be recycled. Therefore, when recycling, specifically which objects as the root object of the accessibility analysis, the virtual opportunity to make decisions based on the situation.
as space is limited, only the accessibility analysis method is introduced here, and the reference counting algorithm I will put in the next article summarizes the introduction.
JVM GC----Garbage tagging algorithm (i)