• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many object are eligible?

 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects are eligible for garbage collection by the time the method f() is invoked?

Result says its 2, but what i think it should be 1, can somebody elaborate how it should be 2 if my concept is wrong?

As i can see there are 3 objectes created and at the end 'c1' is still pointed by z, while 'c2' is lost and 'c3 'is pointed by c1 and c3


 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you are mistaken here that c1 is pointed to c.......Well 1 thing about java is that its pass by copy. So bitpattern used to get to the object it refers is passed not the object. and when c1 = c3 and c2 = c3 happens means c1 and c2 is used to refer the object which is referred by c3. Now the objects referred by c1 and c2 are eligible for GC. The reference z inside the objects are reference variables which reside in the heap and thus cannot be used from the outside world or from a live thread.

gc questions use pen and paper to solve.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UseCodeTags explains how to use code tags correctly; use [ and ] instead of < and >, or just use the "Code" button above the editing window.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but as i was not giving a code so i din't use any [code] to explain it theoretically.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did this code some from? Was it K&B? Quote your sources.

Lines 18-19
c1 --> (MyClass)
c2 --> (MyClass)

Lines 20-21
c1 --> (MyClass - with z --> c2)
c2 --> (MyClass - with z --> c1)

Line 22
c3 --> (MyClass)

Lines 23-24
c1 --> c3
c2 --> c3
c3 --> (MyClass)
?? --> (MyClass - with z --> c2)[was c1]
?? --> (MyClass - with z --> c1)[was c2]

There is nothing on the stack referring to the objects that c1 and c2 used to refer to, they are both eligible. Answer is 2.

Judging by what you have said before the code, you have misunderstood what was happening. I suggest you go through is again carefully.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitish Bangera wrote:Sorry but as i was not giving a code so i din't use any [code] to explain it theoretically.

My post was for Rajesh, not for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic