• 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

Number of Objects eligible for Garbage Collection

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Question from Whizlabs SCJP5:



I thought the answer would be zero as at LINE1 and LINE2 the other() method
assigns the 'z' reference of each object to the objects referred to by
c1 and c2. So the original objects of c1 and c2 are being refered to by its
'z' variable

However the answer given was that the two objects referred to by c1 and c2
are eligble for garbage collection even thought the have references to each other

Can anyone please explain why they are eligible for garbage collection?


Thanks
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer is "Fails to compile."

What is c3?

And f() is missing an opening brace (if not more).
 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whoops...this is the correct code (i copied it wrong the first time)

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming the following is the corrected code that you want an answer to:



At the point when you make c2 = null; you have lost any reachable reference to the objects c1 and c2 even though they are being referenced from within c1.z and c2.z. This is a classical case of circular reference leading to "islands of isolation". The garbage collector is intelligent enough to realize that these two objects cannot be accessed by any program from the outside world. Hence the two objects will be garbage collected. On uncommenting the call to System.gc(); you may see the finalize() method being called twice and printing (it did in my case):

called
called
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well you posted the correct code while i was writing the modified code

Anyways, the answer is the same. Here your actual reference variables c1 and c2 are no more pointing to the actual objects but are pointing to a third object referenced by c3.
It has the same effect as setting c1 = c2 = null;
The rest of the explanation is the same.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the correct explanations is like this:

object referenced by c1 and c2 (before assingment to c3) formed so called "island", which means a group of object referencing one to another, but none of them cannot be reached out of the "island".

JVM treats this object group as eligible to garbage collection, so the answer is 2.

Correct me if I'm wrong
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I think happens ( following Henry Wong's graph method)

Hence there are two objects that will be GCed.

Meera


Edit by mw: Added Code Tags to preserve formatting.
[ November 23, 2008: Message edited by: marc weber ]
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The figures have come out garbled after posting but while typing in it was ok. So I apologize for inconvenience.
Thanks,
Meera
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To supplement Meera's efforts at giving a pictorial representation i would refer you to Corey McGlone's article on garbage collection. It's pretty cool...

How Garbage Collection works
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by meera kanekal:
The figures have come out garbled after posting but while typing in it was ok. So I apologize for inconvenience...


I tried to fix it by adding Code Tags. This keeps "extra" spaces from being removed.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic