This question was taken from Kathy Sierra SCJP 1.6. How many objects are eligible for garbage collections?
According to Kathy Sierra's answer, it is C
. That means two objects are eligible for garbage collection. I have given the explanation of the answer. But why is c3
not eligible for garbage collection (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);
c1 = null;
// Do stuff
} }
When // Do stuff
is reached, how many objects are eligible for GC?
Answer:
Short
wrapper object that is also eligible.That's why after line 11, we are eligible for garbage collection 2 object.
As it can be seen in code snippet 1, we assign null to the variable list, in this way the object new ArrayList() stored in the heap memory will be eligible for garbage collection.
The main objective of Garbage Collection is to free heap memory by destroying the objects that don't contain a reference. When there are no references to an object, it is assumed to be dead and no longer needed. So the memory occupied by the object can be reclaimed.
Conditions for a garbage collectionThe system has low physical memory. The memory size is detected by either the low memory notification from the operating system or low memory as indicated by the host. The memory that's used by allocated objects on the managed heap surpasses an acceptable threshold.
Let's break this down line by line:
CardBoard c1 = new CardBoard();
We now have two objects, the CardBoard
c1
points at and the Short
c1.story
. Neither is available for GC as c1
points at the CardBoard
and the story
variable of the CardBoard
points at the Short
...
CardBoard c2 = new CardBoard();
Similar to above, we now have four objects, none of which are available for GC.
CardBoard c3 = c1.go(c2);
We invoke the method go on the CardBoard pointed at by c1
, passing the value of c2
which is a reference to a CardBoard
Object. We null the parameter, but Java is pass by value meaning that the c2
variable itself is unaffected. We then return the nulled parameter. c3
is null
, c1
and c2
are unaffected. We still have 4 objects, none of which can be GC'd.
c1 = null;
We null c1
. The CardBoard
object which c1
previously pointed at now has nothing pointing to it, and it can be GC'd. Because the story
variable inside that CardBoard
object is the only thing pointing at the Short
, and because that CardBoard
object is eligible for GC, the Short
also becomes eligible for GC. This gives us 4 objects, 2 of which can be GC'd. The objects eligible for GC are the ones formerly referenced by c1
and c1.story
.
No object ever existed that c3
points to. The constructor was only called twice, two objects, one each pointed to by c1
and c2
. c3
is just a reference, that has never been assigned anything but the null pointer.
The reference c3
, that currently points to null, won't go out of scope and be removed from the stack until the closing brace at the end of the main method is crossed.
The object originally assigned to c1
is unreachable because the c1
reference was set to null, but the c2
reference has not been changed, so the object assigned to it is still reachable from this scope via the c2
reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With