permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help in garbage collection question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "help in garbage collection question" Watch "help in garbage collection question" New topic
Author

help in garbage collection question

Amrita Chaurasia
Greenhorn

Joined: Jul 04, 2009
Posts: 7
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?


I got this question from examsguide.com free sample questions...
please help
ankur tyagi
Ranch Hand

Joined: Apr 01, 2009
Posts: 49
first an objectod C is created which is referenced by c1.
then c1 is passed into method m() which does nothing but creates another C object that is referenced by c2
and finally a thierd object referenced by c3 is created.
when line c2=c3 is done..
it simply makes c2 refer to the object also referenced by c3..
so there is no reference left to the object that was originally referenced by c2.
so only 1 object is eligible for GC.
hope it helps...
Amrita Chaurasia
Greenhorn

Joined: Jul 04, 2009
Posts: 7
thanks a lot
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134

ankur tyagi wrote:first an objectod C is created which is referenced by c1.
then c1 is passed into method m() which does nothing but creates another C object that is referenced by c2
and finally a thierd object referenced by c3 is created.
when line c2=c3 is done..
it simply makes c2 refer to the object also referenced by c3..
so there is no reference left to the object that was originally referenced by c2.
so only 1 object is eligible for GC.
hope it helps...


I dont think your answere is correct. After line#6 there are no objects eligible for garbage collection. Ref. variables c1,c2,c3 are still refrencing C object.


Thanks & Regards
Sidharth Pallai
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

I dont think your answere is correct. After line#6 there are no objects eligible for garbage collection. Ref. variables c1,c2,c3 are still refrencing C object.

Well there is 1 object eligible for GC. The explanation is correct. c1,c2 and c3 do refer to an object of class C, but the object that c2 pointed to originally will be eligible for GC...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134

Yes Ankit, you are correct, i was just misunderstood.

The object which got created inside the static method lost its reference to c3, which inturn pointing to a different C object.

nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246


I just arranged it for a better view.

Naveen
Ulrika Tingle
Ranch Hand

Joined: Nov 24, 2009
Posts: 92
Amrita Chaurasia wrote:
After line 6, how many objects are eligible for garbage collection?


It's a trick question.

You don't know because you don't know how many eligible objects have already been GCed.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Ulrika,


Please explain your idea about it being a trick question!

Thanks,

Bert

p.s. I don't think it is, because that's how Sun tends to phrase this kind of question, but maybe you've got an insight here...


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
rajanishhh pillai
Greenhorn

Joined: Jun 07, 2010
Posts: 1
Hi ranches,

There will possibly be two objects that will be getting garbage collected, c1 and c2 (old reference,obj1) will be the unlucky ones.


Cheers
Rajanish
 
GeeCON Prague 2014
 
subject: help in garbage collection question