Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
sweety sinha
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this question is from examguide.com
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?

the answer given is 2. how???
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer is 1.

And also I think the source is examsguide.com
[ September 25, 2008: Message edited by: Ankit Garg ]
 
Ananth Chellathurai
Ranch Hand
Posts: 349
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sinha,

1. c1 is never referenced anywhere after C c2=m1(c1) and it is ready to gced.
2. C c2=m1(c1); m1 method results to a new C object whose reference c2 is replaced by c3. c2=c3; the object resulted from m1(c1) is also ready to be gced.

So the answer is 2.

Regards
Ananth Chellathurai
 
James Basller
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sweety sinha:
this question is from examguide.com
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?

the answer given is 2. how???


Hi,

This is because Objects which are going to be garbage collected when there are no more reference are there. So, here first c1,c2,c3 3 objects will be created. Now, at line C c2=m1(c1); when executed then c1 reference will be assigned to the obj1 so c1 is now free and it has no reference now.

Then, at line c2=c3 reference of c3 is assigned to c2 so now c3 is also free.

So, at this position there will be 2 objects c1 and c3 are eligible for garbage collection. So, the answer is 2
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all of you remember, objects are not passed as references of C++ but pointers of C++.

So c1 is not going to be garbage collected...
 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Ankit.

only the object created inside the method m1 will be available for GC.
c1 is not assigned a null value so still the object created at line 1 is referenced by c1. Hence, it is not eligible for garbage collection.

So the answer should be 1.
 
sweety sinha
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only the object created inside the method m1 will be available for GC.

why??
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The object referenced by c2 will be eligible for garbage collection

that is the object created as

C c2 = m1(c1);

m1 method will return a new object of class C. That object will be eligible for garbage collection at line 6.
[ September 25, 2008: Message edited by: Ankit Garg ]
 
Lakshman Arun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be more precise, the object referenced by c2 which was created inside the method m1, is the only object available for GC
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I want to know is after the method m1 returns, does the reference pointed by ob1 would no longer be there?

If this is the case, then yes, the object created inside method m1 would only have a single reference (c2) and that also changes in line 6, where c2 refers to the object referred by c3.
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Paul, you are right...
 
Manju Kavi
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sweety sinha:
this question is from examguide.com
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?

the answer given is 2. how???
 
Manju Kavi
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think only 1 object is eligible after line 6.
that is that object created in m1 method. As we know all local variables die after method ends, so ob1 will be lost. And at line 6 c2 will refer to c3..
 
sweety sinha
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all for your cooperation
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Even i agree with Ankit. Only one object is eligible for Garbage Collection. And one more thing, In Java, everything is call by Value.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic