• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How many object are eligible?

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic