In-depth understanding of Java Virtual Machine notes---determine if objects survive

Source: Internet
Author: User

The heap contains almost all of the object instances in the Java world, and the first thing the garbage collector does before it recycles the heap is to determine which of these objects are "alive" and which objects are "dead" (that is, objects that are no longer possible to be used by any means).

1. Reference counting algorithm
Many textbooks determine whether an object survives the algorithm is this: Add a reference counter to the object, whenever there is a place to reference it, the counter value plus 1, when the reference fails, the counter minus 1, any time the counter is 0 of the object is impossible to be used again.
The reference counting algorithm (Reference counting) is simple to implement and the judging efficiency is very high, in most cases it is a good algorithm. However, the Java language does not use the reference counting algorithm to manage memory, one of the main reasons is that it is difficult to solve the problem of circular reference between objects.

For example: in the TESTGC () method, the object Obja and OBJB have field instance, assignment to OBJA.INSTANCE=OBJB and Objb.instance=obja, except that the two objects have no references. In fact, both objects are no longer accessible, but because they refer to each other as the object side, the reference count of the exception is not 0, and the reference counting algorithm cannot tell the GC collector to reclaim them.


public class REFERENCECOUNTINGGC {public Object instance = null;private static final int _1MB = 1024 *1024;/** * Just to occupy a bit of memory */private byte[] bigsize = new byte[2 * _1MB];p ublic static void Main (string[] args) {REFERENCECOUNTINGGC obja = new Refer ENCECOUNTINGGC (); REFERENCECOUNTINGGC OBJB = new REFERENCECOUNTINGGC () obja.instance = Objb;objb.instance = Obja;obja = Null;objB = null;//false The GC is located here, then whether Obja and OBJB will be recycled System.GC ();}}

The result of the operation is:

[GC [defnew:234k->64k (960K), 0.0009447 secs][tenured:2125k->2189k (4096K), 0.0048757 secs] 2282k->2189k ( 5056K), [perm:365k->365k (12288K)], 0.0058659 secs] [times:user=0.00 sys=0.00, real=0.00 secs]
[Full GC (System) [tenured:4237k->141k (6148K), 0.0052656 secs] 4237k->141k (7108K), [perm:365k->365k (12288K)] , 0.0052973 secs] [times:user=0.01 sys=0.00, real=0.01 secs]
Heap
def New Generation Total 960K, used 18K [0x23b10000, 0x23c10000, 0x23ff0000)
Eden Space 896K, 2% used [0x23b10000, 0x23b14818, 0x23bf0000)
From space 64K, 0% used [0x23c00000, 0x23c00000, 0x23c10000)
To space 64K, 0% used [0x23bf0000, 0x23bf0000, 0x23c00000)
Tenured generation total 6148K, used 141K [0x23ff0000, 0x245f1000, 0x27b10000)
The space 6148K, 2% used [0x23ff0000, 0x240136b8, 0x24013800, 0x245f1000)
Compacting Perm Gen Total 12288K, used 365K [0x27b10000, 0x28710000, 0x2bb10000)
The space 12288K, 2% used [0x27b10000, 0x27b6b578, 0x27b6b600, 0x28710000)
Ro space 8192K, 63% used [0x2bb10000, 0X2C023B48, 0X2C023C00, 0x2c310000)
RW Space 12288K, 53% used [0x2c310000, 0x2c977f38, 0x2c978000, 0x2cf10000)


In the running results can be seen in the GC log contains "4237k->141k", the old age from 4273K (about 4M, in fact, Obja and OBJB) into 141K, meaning that the virtual is not because the two objects are referenced by each other, do not reclaim them, This also proves that virtual is not by reference counting algorithm to determine whether an object survives. We can see the object into the old age, but we all know that the object was just created in the new generation, the default age to enter the old era to 15, but here Obja and OBJB but entered the old age. This is because the Java heap area will grow dynamically, at first the heap area is small, the object enters the old age also has a rule, when the survior space in the same generation object size and more than half of the Survior space, the object will go directly to the old age.


2. Root Search algorithm
In mainstream commercial programming languages (Java and C #), the root search algorithm (GC Roots tracing) is used to determine whether an object survives. 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, the path of the search is called the reference chain (Reference Chain), when an object to the GC Roots no reference chain connected, It proves that this object is not available, such as:


Object Bject5, OBJECT6, object7 although they have mutual judgment, they are unreachable to GC roots, so they will be judged as recyclable objects.
In the Java language, the following types of GC roots objects are included:
A. Referenced objects in the virtual machine stack (local variable table in the stack frame)
B. Objects referenced by class static properties in the method area
C. Objects referenced by constants in the method area
D. Objects referenced by JNI in the local method stack

In-depth understanding of Java Virtual Machine notes---determine if objects survive

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: info-contact@alibabacloud.com 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.