This week's book giveaway is in the Object Relational Mapping forum.
We're giving away four copies of Pro JPA 2: Mastering the Java Persistence API and have Mike Keith and Merrick Schincariol on-line!
See this thread for details.
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
Author

help in garbage collection question

Amrita Chaurasia
Greenhorn

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

This message was edited 1 time. Last update was at by Amrita Chaurasia

Henry Wong
author
Bartender

Joined: Sep 28, 2004
Messages: 9919

Please QuoteYourSources[/surl]]Quote Your Sources.

Thanks,
Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
ankur tyagi
Greenhorn

Joined: Apr 01, 2009
Messages: 24

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
Messages: 7

thanks a lot
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Messages: 125

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.

With Regards
Sidharth Pallai
Ankit Garg
Bartender

Joined: Aug 03, 2008
Messages: 6293

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.0 98%, SCWCD 5 98%, Javaranch SCJP FAQ, SCWCD Links
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Messages: 125

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.


With Regards
Sidharth Pallai
nav katoch
Ranch Hand

Joined: May 02, 2008
Messages: 230


I just arranged it for a better view.

Naveen

This message was edited 1 time. Last update was at by nav katoch

Ulrika Tingle
Ranch Hand

Joined: Nov 24, 2009
Messages: 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
Messages: 7457

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

350 p.p.m. If you're not on the edge, you're taking up too much room.
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
hibernate profiler