• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

GC program needs explanation

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

class A{
A aob;
public static void main(String args[]){
A a=new A();
A b=new A();
A c=new A();
a.aob=b;
b.aob=a;
c.aob=a.aob;
A d=new A().aob=new A();
c=b;
c.aob=null;// line1
System.gc();
}
}


Question is how many objects are available for GC after line1;

Output is 2. Can anybody explain this to me
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please QuoteYourSources when posting questions
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priyadarshini,

can you please tell us what is the answer as per your understanding?

I believe that the answer which you have mentioned is not correct.

Thanks,
Hemnath
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think 2 is correct.


- First you create 3 Objects and their 3 references a b c:

A a = new A();
A b = new A();
A c = new A();




- than you link the objects:

a.aob = b;
b.aob = a;
c.aob = a.aob;



After this one Object is referenced by a and b.aob.
one Object is referenced by b and a.aob
one Object is referenced by c


Than they try to get you confused:
- 2 Objects are created.
- The First gets a reference to the Second
- After this d gets the result of this operation (The result of the assignment is the assigned Object- so the 2nd Object is reffered)
- the First Object is available for the gc

A d = new A().aob = new A();




Now they set

c = b;



So the Object referenced by c is now ready for the GC (it now also references the Object that is referenced by b and a.aob)


This is senceless:

c.aob = null;


Because there is another reference to the Object (a)






 
priyadharshini vijayaraghavan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I forgot the place from where I got this program... coz i was searching for GC related questions in internet.
I thought i understood GC very well and so i wanted to test myself, but unfortunately I found this one very difficult to solve. I got confused.

I thought only the object referenced by c will be eligible for GC.

but now I understood.

Thanks micha

It was a good explanation.










 
Hemnathbabu Kottaiveedu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very well explained Micha!!. I agree with you.

Thanks,
Hemnath
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic