aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC Question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC Question." Watch "GC Question." New topic
Author

GC Question.

Amisha Shah
Ranch Hand

Joined: Mar 03, 2006
Posts: 33
program:

class C

{

public static void main(String a[])

{

C c1=new C();

C c2=m1(c1);

C c3=new C();

c2=c3; //6

anothermethod();

}

static C m1(C ob1){

ob1 =new C();

return ob1;

}

}

After line 6, how many objects are eligible for garbage collection?


Amisha Shah.<br />SCJP 1.4
Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
en, three objects created in the program
c1,c3,ob1

after line 6
ob1 is unreachable ,hence ob1 is eligible for gc
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Amisha,

Changchun is correct. After line 6 the object reference created from within m1 method and returned to C2 variable cannot be reached anymore.


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
vivek yadav
Greenhorn

Joined: Apr 16, 2006
Posts: 8
hello Amisha,
Only one object will be ready for garbage collection.
Wen you assign c2=c3, the reference to ob1 is lost and
its now ready for collection, garbage collection.


Vivek Kumar Yadav
SCJP, SCWCD
bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 176
Amisha,

3 objects become eligble for gc, first c3 dies due to end of method,

c1 dies in method m1 method


the object created in m1 method i.e obj1 dies in main method due to c2=c3.

Hope it is clear...


Kiran Kumar.
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Kumar,

I don't think you're correct. The question asks how many objects will be eligible for GC after line 6

After such line the main method() is still running and have not ended.
[ April 17, 2006: Message edited by: Edisandro Bessa ]
bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 176
Ok sorry i have not read question properly, in general that is the answer if we consider upto line 6 then you are right
mohamed ewis
Greenhorn

Joined: Feb 05, 2006
Posts: 18
hi Edisandro
you are correct only object created in m method eligible for gc
Amisha Shah
Ranch Hand

Joined: Mar 03, 2006
Posts: 33
hi all,

here , method is returning object so how that object is eligible for garbage colection ? i read in k&b at page 434(Reassining a reference variable) object return by method is not eligible for garbage collection.

so i think , there is no object eligible for garbage collection. is there anyone who think so ?
magesh sripathy
Greenhorn

Joined: Apr 06, 2006
Posts: 27
No amisha u r wrong...
yeah ur partially correct...method is returning the object reference and is set to c2 ..so at that time object created in m1 method is not eligible for garbage...Then have a look at this code..

C c3=new C();
c2=c3; //6
so a new object referenced by c3 is created..and then c2 is made to refer to this new created object....

So the object previously refered by c2 become eligible for gabrage collection...

Hope it would have Cleared

-----------------------
Magesh.s
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC Question.