It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection" Watch "Garbage collection" New topic
Author

Garbage collection

Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
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
jean-gobert de coster
Ranch Hand

Joined: Dec 04, 2008
Posts: 49
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18504
    
  40

The answer is correct -- two objects are eligible. And it's not too difficult to draw it out, just give it a try.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
I found TWO objects eligible. But actually I don't know how to explain this. :roll:
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

@ 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).


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Hmm , thanks Ankit.
Next time i wll try that.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


[ December 16, 2008: Message edited by: Punit Singh ]
[ December 16, 2008: Message edited by: Punit Singh ]

SCJP 6
jean-gobert de coster
Ranch Hand

Joined: Dec 04, 2008
Posts: 49
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

Joined: Sep 07, 2008
Posts: 265
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

Joined: Sep 07, 2008
Posts: 265
Hi James,

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

thanks to all.

Preetha
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
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

Joined: Sep 07, 2008
Posts: 265
Hi,

the second object is OA4object.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks..Preetha.
 
Consider Paul's rocket mass heater.
 
subject: Garbage collection
 
Similar Threads
How to make the map of object to be garbage collected
exam lab gc question
How many Objects are elligible for Gabbage Collection at Line 17?
Garbage collection
Garbage Collection