Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objects eligible for garbage collection

Tags:

java

scjp

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?

  • A: 0
  • B: 1
  • C: 2
  • D: Compilation fails
  • E: It is not possible to know
  • F: An exception is thrown at runtime

Answer:

  • C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.
  • A, B, D, E, and F are incorrect based on the above. (Objective 7.4)
like image 541
user414967 Avatar asked Jul 19 '12 15:07

user414967


People also ask

How many objects are eligible for garbage collection after 11 runs?

That's why after line 11, we are eligible for garbage collection 2 object.

Which of the following code snippets makes objects eligible for garbage collection?

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.

What is the main object of 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.

What are the three possible conditions for a garbage collection?

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.


2 Answers

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.

like image 155
Jacob Raihle Avatar answered Oct 18 '22 23:10

Jacob Raihle


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.

like image 23
Affe Avatar answered Oct 18 '22 23:10

Affe