Question about Garbage Collection for my OCA Exam

Viewed 180
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;
// When this line is reached, what is eligible for the GC.
    }
}

Can somebody please explain what happens here? I understand that c1 and the Short are eligible for the Garbage Collector. But what happens with c2 and why isn't c3 also available for it? I might oversee something here but I would be glad for every input that helps me get around this topic. My OCA exam is next week and I still have trouble with this kind of stuff.

Edit: The comment of what is eligible for the GC.

2 Answers

Considering Java is pass-by-value, we know that Java isn't passing the object, it's passing a reference to the object.

Because of this, when you did CardBoard c3 = c1.go(c2);, the method go(Cardboard cb) just copied the reference value of c2 to cb and therefore c2 is unharmed by the cb = null;. So, c2 is not eligible for GC.

c3 never gets to be constructed as an object, it is just a variable of type CardBoard initialized to null. Thus, it cannot be garbage collected because it is not even created on the heap.

Perhaps visualizing the objects involved will help. After the first statement in main, we have:

         +-----------+        +-------+
         | CardBoard |   ---> | Short |
c1 ----> |-----------|  /     |-------|
         |  story    | /      |  200  |
         +-----------+        +-------+

After the second statement in main, we have:

         +-----------+        +-------+
         | CardBoard |   ---> | Short |
c1 ----> |-----------|  /     |-------|
         |  story    | /      |  200  |
         +-----------+        +-------+

         +-----------+        +-------+
         | CardBoard |   ---> | Short |
c2 ----> |-----------|  /     |-------|
         |  story    | /      |  200  |
         +-----------+        +-------+

(theoretically, it's also possible that the two CardBoard share the Short object, the Javadoc of Short.valueOf says:

This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

However, the JDK implementation only caches in the range -128 to 127, so we are going to assume that going forward)

Upon entering the go() method, we have:

         +-----------+        +-------+
         | CardBoard |   ---> | Short |
c1 ----> |-----------|  /     |-------|
         |  story    | /      |  200  |
         +-----------+        +-------+

         +-----------+        +-------+
         | CardBoard |   ---> | Short |
c2 ----> |-----------|  /     |-------|
     --> |  story    | /      |  200  |
    /    +-----------+        +-------+
   / 
  /
cb

and before leaving go(), we have:

         +-----------+        +-------+
         | CardBoard |   ---> | Short |
c1 ----> |-----------|  /     |-------|
         |  story    | /      |  200  |
         +-----------+        +-------+

         +-----------+        +-------+
         | CardBoard |   ---> | Short |
c2 ----> |-----------|  /     |-------|
         |  story    | /      |  200  |
         +-----------+        +-------+
    
  
cb --x    

and after the 3rd statement in main, we have:

         +-----------+        +-------+
         | CardBoard |   ---> | Short |
c1 ----> |-----------|  /     |-------|
         |  story    | /      |  200  |
         +-----------+        +-------+

         +-----------+        +-------+
         | CardBoard |   ---> | Short |
c2 ----> |-----------|  /     |-------|
         |  story    | /      |  200  |
         +-----------+        +-------+
    

c3 --x

And before leaving main(), we have:

         +-----------+        +-------+
         | CardBoard |   ---> | Short |
c1 -x    |-----------|  /     |-------|
         |  story    | /      |  200  |
         +-----------+        +-------+

         +-----------+        +-------+
         | CardBoard |   ---> | Short |
c2 ----> |-----------|  /     |-------|
         |  story    | /      |  200  |
         +-----------+        +-------+
    

c3 -x    

As we can see, of the 4 objects allocated, 2 remain reachable, and 2 are eligible for collection.

Related