Java – cross reference and garbage collection

There is an extensive object graph application The graph mainly consists of a set of subgraphs connected to the rest of the graph by a unique reference However, each such internal subgraph has some cross references between objects There was a time when such a subgraph needed to be discarded Can you only set enough to point unique references to this subgraph to qualify for garbage collection?

My concern is that internal cross references may "protect" the entire subgraph from garbage collection In other words, does the garbage collector understand enough to ensure that all references in the subgraph do not leave the boundary of the subgraph, so the whole subgraph can be cleared

Solution

As stated in this so question, circular references are well managed

Java does not do reference counting, it does "mark and scan" garbage collection If you follow all the activity references, find out what objects are "reachable" and clean up everything else

References that can be accessed by the object itself do not affect accessibility, so if they are empty, they are not important

The only case where setting a reference to null might make sense is to discard a very large object in the middle of a long - running method

In this case, setting null as the reference of the graph will help to form an isolated island (even an internal circular reference), as described in this article

You will find more details about inaccessible status in the truth about garbage collection:

cannot access

When no stronger reference exists, the object enters an unreachable state When an object is inaccessible, it is a candidate

Note the wording: just because an object is a collection candidate does not mean that it will be set immediately The JVM is free to defer collection until an object is needed immediately

It is important to note that not only powerful references will occupy an object in memory These must be references from the garbage collection root link GC root is a variable of a special class, including:

>Temporary variables on the stack (any thread) > static variables (from any class) > special references to JNI native code

Circular strong references do not necessarily lead to memory leaks Consider a code that creates two objects and assigns them to each other's references

public void buidDog() {
   Dog newDog = new Dog();
   Tail newTail = new Tail();
   newDog.tail = newTail;
   newTail.dog = newDog;
}

Before the method returns, the temporary stack variable in the builddog method has a strong reference to "dog" and "tail"

After the builddog method returns, both dog and tail are inaccessible from the root directory and are candidates for collection (although the VM may not be able to collect these objects in real time for an indefinite period of time)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>