Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help in garbage collection question

 
Amrita Chaurasia
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot
 
Sidharth Pallai
Ranch Hand
Posts: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Sidharth Pallai
Ranch Hand
Posts: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 246
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I just arranged it for a better view.

Naveen
 
Ulrika Tingle
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
rajanishhh pillai
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic