• 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

Garbage collection doubt

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

I found this question in whizlab test

class TestGC{
TestGC aRef;
public static void main(String args[])
{
TestGC a = new TestGC();
TestGC b = new TestGC();
TestGC c = new TestGC();
a.aRef = b; //->line 1
b.aRef = c; //->line 2
c.aRef = a; //->line 3

c = new TestGC();
a=b=c; //->line 4
system.gc();
}}


So After line 4 , how many objects will eligible for garbage collection?

Ans:-3, but i got confused due to line 1,2,3. aRef is still reffreing to all three objects.
Please explain what is happening at line1,2,3?

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

The class TestGC has a "Has A"-relationship with itself. So when you reach line 1, three objects have been created.

At line 1, we make the aref variable of a refer to b.
At line 2, we make the aref variable of b refer to c.
At line 3, we make the aref variable of c refer to a.

After line 3, there are still just 3 objects.

Then we refer c to a new instance of TestGC wich means there are now 4 objects created. None of these objects are eligible for Garbage Collection because the original c is still referenced by b.

At line 4 we make a and b refer to the same object as c. At this point 3 objects become eligible for Garbage Collection. Object a, b and the original c (wich was still referenced by b).
[ May 09, 2007: Message edited by: Jesse Custer ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


It is problem of ISLANDIC objects. JVM handles it well. ICELANDIC objects
don't have active reference to be used in the program but still have some reference that refer to them, but those references can be get.


In this case each estGC object has encapsulated another TestGC object
reference variable, and later on you have made three objects created in
your code refer to each other. Till now there is no problem. But when there
remains no reference to refer to those objects, they are eligible for
GC as JVM understands it well.

c = new TreeGC(); //Line #1
a=b=c; // it is the point when all previously created objects (except Line #1 of course) become eligible for GC.


BTW, DO quote source of question


Thanks,
 
Gunjan Kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
I have mentioned the source of question on very first line.

Source: WhizLab Test
 
reply
    Bookmark Topic Watch Topic
  • New Topic