aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection 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 "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
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

Joined: Aug 03, 2008
Posts: 9303
    
  17

I think the answer is 1.

And also I think the source is examsguide.com
[ September 25, 2008: Message edited by: Ankit Garg ]

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ananth Chellathurai
Ranch Hand

Joined: Nov 21, 2007
Posts: 349

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


Ananth Chellathurai [Walk on software]
James Basller
Ranch Hand

Joined: Sep 07, 2008
Posts: 58
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

Joined: Aug 03, 2008
Posts: 9303
    
  17

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

Joined: Aug 27, 2008
Posts: 137
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.


Thanks,<br />Srilatha M
sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
only the object created inside the method m1 will be available for GC.

why??
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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

Joined: Sep 24, 2008
Posts: 19
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

Joined: May 19, 2008
Posts: 177
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.


Preparing for SCJP 6.0
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Yes Paul, you are right...
Manju Kavi
Ranch Hand

Joined: Sep 25, 2008
Posts: 33
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

Joined: Sep 25, 2008
Posts: 33
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

Joined: Jul 07, 2008
Posts: 76
thanks all for your cooperation
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

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.


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection