What objects are eligible for GC?

class CardBoard { Short story = 200; CardBoard go(CardBoard cb) { cb = null; return cb; } public static void main(String[] args) { CardBoard c1 = new CardBoard(); CardBoard c2 = new CardBoard(); CardBoard c3 = c1.go(c2); System.out.println("c3 value : "+c3); c1 = null; System.out.println("c1 value : "+c1); System.out.println("c2 value : "+c2); // do Stuff } } 

This is an example from the SCJP6 layout exam. This question says: when // doStuff is reached, how many objects are eligible for GC? And the answer (2 objects), because: only one CardBoard object (c1) is eligible, but it has an associated Short which is also eligible to participate.

When I execute the code, it looks like c3 is also pointing to null ... so I would say that 3 objects are eligible for GC.

Can someone please guide me through the logic of this code.

+4
source share
4 answers

The c3 object is initially zero, so there is no need to recall it, since it never existed in the first place. The garbage collector is designed to remove objects that really exist on the heap.

Otherwise, the link to c2 never dropped, and therefore it will not be fixed. Although it appears that c2 is invalidated in the CardBoard c3 = c1.go(c2); statement CardBoard c3 = c1.go(c2); , this is not true. The c2 reference was passed by value, and although the reference is nullified, there is an existing object reference in the main method. Therefore, it will not be fixed.

This leaves us with c1 , which was explicitly revoked and therefore entitled to collection. However, c1 also contains a link to the Short variable history, which has no inbound links from any other object. This leads to the fact that two objects have the right to clear - one CardBoard object and the built-in Short object.

+10
source

CardBoard c1 = new CardBoard ();

Creates an instance of CardBoard and its instance of Short . (2 objects) Assigns a CardBoard link to c1 .

CardBoard c2 = new CardBoard ();

Creates another CardBoard instance and its Short instance. (2 more objects) Assigns a CardBoard link to c2 .

CardBoard c3 = c1.go (c2);

Assigns null to c3 . (The go method is a trick to figure out whether you understand the semantics of passing Java arguments. If you think Java uses pass-by-reference, you might mistakenly conclude that this c2 is null . Fact, c2 not changed.)

c1 = null;

Assigns null to c1 . This makes the first instance of CardBoard and its instance of Short inaccessible, and candidates for garbage collection.

A second instance of CardBoard and its instance of Short are still available.

+2
source

c3 never points to any object; it is always an empty variable. Consequently, c3 is not suitable for GC

+1
source

In your code, only c1 and story (Short) contained in c1 are eligible for GC. , i.e. 2 objects.

c3 never refers to any object, since the go () method returns null.

c2 is not allocated for GC. As with the Java argument, arguments are passed by value. and when the go method is called c2, it continues to point to the object, although a variable is passed to the go method that is set to null.

0
source

All Articles