• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Garbage Collection

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 349
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
only the object created inside the method m1 will be available for GC.

why??
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Paul, you are right...
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all for your cooperation
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic