Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Garbage collection - orphaned LinkedList links

Suppose you have references A -> B -> C -> D. When you delete the reference to B from A, you're left with an orphaned chain of Objects B -> C -> D.

Will C and D be garbage collected even though there's no way to get to them (since there's no reference to B)?

I imagine the GC is smart about this and will resolve any such dependencies.

However, I took a look into the source code for the LinkedList class and found something contrary to this belief. I noticed that when a list is clear()ed, all of the references to each link are explicitly set to null, thus making it an O(n) operation. Is there any reason/benefit for doing so?

like image 793
tskuzzy Avatar asked Aug 04 '11 02:08

tskuzzy


People also ask

What is garbage collection in linked list?

Garbage collection (GC) is a dynamic technique for memory management and heap allocation that examines and identifies dead memory blocks before reallocating storage for reuse.

How does garbage collector remove unused references?

As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory.

Does PHP have garbage collection?

As of PHP 7.3, PHP provides basic garbage collection information in user land PHP, via gc_status. The function returns: The number of garbage collection runs. The number of objects collected.

Can we guarantee garbage collection in Java?

No, Garbage collection does not guarantee that a program will not run out of memory. The purpose of garbage collection (GC) is to identify and discard objects that are no longer needed by a Java program, so that their resources can be reclaimed and reused.


1 Answers

That does look a bit peculiar. Maybe the reason that it is explicitly dismantling the list is so that the list is cleared for existing iterators and sublists as well as the parent list.

It is certainly NOT done to make the garbage collection faster. A garbage collector doesn't traverse the references in an unreachable object, so nulling them won't make any difference.

UPDATE

A more recent version of the method has these comments:

// Clearing all of the links between nodes is "unnecessary", but:
// - helps a generational GC if the discarded nodes inhabit
//   more than one generation
// - is sure to free memory even if there is a reachable Iterator

So, it appears that there is an benefit for the GC, at least in some cases.

Suppose that a Node in an older generation contains a reference to an object (e.g. a Node or an element) in a younger generation. That reference becomes a "root" when collecting of the younger generation, causing the young generation object to be retained, even if the old generation Node is unreachable. This state continues until the older generation is collected. Old generations are collected infrequently.

If you traverse the list and dismantle it, the variable containing the old -> new reference is assigned a null. The write-barrier for that assignment causes (immediately or at GC time) the original reference to no longer be a "root". Thus, the object in the younger generation can now be collected, and it doesn't end up "tenured" to an older generation (which brings forward the time when that generation needs to be collected).

Presumably, the GC benefits outweigh the cost of unpicking the list ... either on average, or in cases where the costs are disastrous.

For more information, refer to "Garbage Collection algorithms for dynamic memory management" by Jones and Lins. It is in chapter 7.5 in my (first edition) copy.


Generally speaking, it is better to throw a Collection object away and start again than it is to clear it for reuse.

like image 138
Stephen C Avatar answered Oct 14 '22 03:10

Stephen C