| Author |
Number of Objects eligible for Garbage Collection
|
Santiago Bravo
Ranch Hand
Joined: Jul 25, 2008
Posts: 226
|
|
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
|
Santiago
My Path to SCJP Certification My Path to SCWCD Certification
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Answer is "Fails to compile." What is c3? And f() is missing an opening brace (if not more).
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Santiago Bravo
Ranch Hand
Joined: Jul 25, 2008
Posts: 226
|
|
whoops...this is the correct code (i copied it wrong the first time)
|
 |
Harvinder Thakur
Ranch Hand
Joined: Jun 10, 2008
Posts: 231
|
|
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
|
thanks
Harvinder
|
 |
Harvinder Thakur
Ranch Hand
Joined: Jun 10, 2008
Posts: 231
|
|
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.
|
 |
Lukasz Picur
Greenhorn
Joined: Nov 22, 2008
Posts: 1
|
|
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
|
 |
meera kanekal
Ranch Hand
Joined: Feb 13, 2005
Posts: 75
|
|
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
Joined: Feb 13, 2005
Posts: 75
|
|
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
Joined: Jun 10, 2008
Posts: 231
|
|
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
Joined: Aug 31, 2004
Posts: 11343
|
|
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.
|
 |
 |
|
|
subject: Number of Objects eligible for Garbage Collection
|
|
|