Java Object Survival Analysis--Reference counting method & Accessibility Analysis

Source: Internet
Author: User

The Java Virtual machine is divided into five areas, of which three are thread-private: program counters, virtual machine stacks, local method stacks , two threads sharing: heap, method area . The thread-private area is automatically freed when the thread ends (stack frames are out of stack), and the space is easier to clean up. And the thread-shared Java heap and method areas are large in space and there is no thread of recycling easy to generate a lot of junk information, GC garbage collection is really concerned about this part.

The Java heap and method area are primarily housed in various types of objects (the method area also stores information such as static variables and global constants), so the first thing we should consider when using GC to recycle it is how to determine if an object should be reclaimed . This is to determine whether an object has other references or associations that make the object live . We need to mark all objects that are not in existence in order for the GC to be recycled.

There are two kinds of common methods to judge whether the object survives or not: reference counting method and the algorithm of accessibility analysis . Reference Counting Method

The logic of the reference counting method is very simple, but there is a problem, Java does not use this way to make object survival judgments.

The logic of the reference notation is to maintain a counter counter at the head of the object when it is stored in the heap, and counter++ if an object adds a reference to it. counter– If a referential relationship is invalidated. If an object's counter becomes 0, the object is discarded and is not in a viable state.

There are a number of problems with this approach to labeling the state of an object:

1 The JDK has added a variety of references starting from 1.2: Soft references, weak references, virtual references, and the program should perform different operations under different references. If we only use a reference counting method to count the cases that cannot accurately distinguish between so many references.

Reference counting does not solve problems with multiple types of references. But this is not fatal, because we can differentiate the four references by adding logic, although some of the trouble is still a variant of the reference notation, which really makes the reference counting method completely obsolete below.

2 If an object a holds object B, and Object B also holds an object A, that occurs when a deadlock in a similar operating system is held, in which case the counter of A and B are greater than 1, and the GC will never be able to recycle the two objects. The algorithm of accessibility analysis

In the mainstream commercial programming languages (Java and C #), the Accessibility analysis algorithm is used to determine whether an object survives. The basic idea of this algorithm is to use a series of objects named GC Roots as the starting point, starting down the search from these nodes, the path traversed by the search is called the reference chain (Reference Chain), when an object to the GC Roots is not connected by any reference chain, It is proved that this object is not available, the following figure object Object5, OBJECT6, OBJECT7, although there is mutual judgment, but they to the GC roots is not up to, so they will be judged to be recyclable objects.

So those dots can be used as GC roots. In general, objects in the following situations can be referred to as GC Roots: Objects in the object method area referenced by a class static property in a referenced object method area in the virtual machine stack (a local variable table in the stack) reference to a JNI (native method) in the objects local method stack How to realize the algorithm of hotspot virtual machine.

The mainstream virtual machine in Java hotspot uses the accessibility analysis algorithm to determine the state of an object, then hotspot what structure is used to implement the algorithm.

use Oopmap to record and enumerate root nodes

Hotspot first need to enumerate all GC roots root node, the virtual machine stack space is small, traverse time may be acceptable, but the method area space is likely to have hundreds of trillion, traversing once takes a long time. More crucially, when we traverse all GC roots root nodes, we need to pause all user threads because we need a "virtual machine snapshot" At this point, and if we do not pause the user thread, the VM is still running and we cannot ensure that all root nodes are properly traversed. So the time overhead is too much for us to accept.

Based on this situation, hotspot implements a data structure called OOPMAP, which calculates the type of offset within an object when the class is loaded, and stores the position, and accesses all oopmap when the root node needs to be traversed.

safepoint constraint root node with security point

If each object that conforms to the GC roots condition is deposited into the oopmap, then Oopmap becomes large, and many of these objects are likely to change, making it difficult to maintain the mapping table. In fact, hotspot does not create oopmap for each object, it is created at a specific location called a security point (safepoints).

In order to ensure that the number of safe points in the virtual machine is not too much or too little, it is time that determines whether the safety point is established. When time-consuming operations occur, such as method calls, loop jumps, and so on, create a security point. In addition, the Hotspot virtual machine has added the concept of safe zone on the basis of the safety point, the security area is the extension of the security point. The effect that a security point cannot achieve in a security area.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.