• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Garbage collection

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, this question is from Devaka's practice exam3
class A{

A a1;
A a2;

public void finalize(){
System.out.println("-");
}

public static void main(String args[]){
A s1=new A();
s1.a1=new A();
s1.a2=s1;
s1.a1.a2=new A();
s1.a1.a2.a2=new A();
s1.a1.a2.a2.a1=s1.a1;
s1.a2.a1.a2=null;
System.gc();
}

}

Please explain me a good way to draw picture for this type of question.
answer is 2 obbjects are eligible.

Thanks in advance
Preetha
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hum... I can only find one... First thing you can notice is that there is only one place in the code where a reference is modified:

s1.a2.a1.a2=null

so when you look at what was referenced by this, you'll see that it was a new A()

and also, there is later no reference made to s1.a2.a1.a2 so that instance of A becomes GC-able. But I don't see any other instance of A not being referenced any more.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is correct -- two objects are eligible. And it's not too difficult to draw it out, just give it a try.

Henry
 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found TWO objects eligible. But actually I don't know how to explain this. :roll:
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Preetha Arun ,

I was wondering how to post a image in the forum.
So ,i have send you a mail(yahoo).Check your mail.
Hope that makes things clear.
If anybody know how to post image ,please let me know.(Where the image is in my home pC).
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James you cannot include images on javaranch from your hard disk. You will have to upload the image somewhere. I use flickr to upload the images and put them here...
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm , thanks Ankit.
Next time i wll try that.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


[ December 16, 2008: Message edited by: Punit Singh ]
[ December 16, 2008: Message edited by: Punit Singh ]
 
jean-gobert de coster
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh yes, I got confused by the fact that s1.a2.a1.a2 was actually the same thing as s1.a1.a2

Indeed, not doing this with the picture makes things more complicated.

I hate those kind of questions that make you waste precious time on the exam.
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, i am just trying to draw the logic here....


1.A s1=new A();

s1--------------->OA1object

2.s1.a1=new A();

s1.a1------------>OA2object(i.e OA1object.a1)

3.s1.a2=s1;

s1--------------->OA1object
^
|
s1.a2----------------

4.s1.a1.a2=new A();

s1.a1.a2---------->OA3object(i.e OA2object.a2)

5.s1.a1.a2.a2=new A();

s1.a1.a2.a2-------->OA4object(i.e OA3object.a2)

6.s1.a1.a2.a2.a1=s1.a1;



7.s1.a1.a2.a2.a1=s1.a1;


s1.a1-------------->OA2object
^
|
s1.a1.a2.a2.a1----------

8.s1.a2.a1.a2=null

s1.a2=s1;

s1.a1.a2=null(i.e OA3object reference is null,so OA3object is eligible for GC)

OA4object is created by invoking OA3object ,since OA3object lost it's reference oA4object is also eligible for Gc


This is what i understood from this program. Please anyone tell me ,is this correct or not?! :roll:

Thanks in advance
Preetha

[ December 16, 2008: Message edited by: Preetha Arun ]
[ December 16, 2008: Message edited by: Preetha Arun ]
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James,

Thanks for taking such an effort to clear my question.
I am getting clear with it.

thanks to all.

Preetha
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Preetha, I was calculating the no of objects eligible for GC the same way you have tried in the above post, so can you please tell me which is the 2nd object for GC?
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

the second object is OA4object.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks..Preetha.
 
reply
    Bookmark Topic Watch Topic
  • New Topic