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

vini singh
Greenhorn

Joined: Dec 04, 2008
Posts: 18
Assume that there is a class MyClass and it have 2 functions f() and g().

MyClass c1=new MyClass();
MyClass c2=new MyClass();
f(c1);
c1=null;
c2=c1;
g();

How many objects will be eligible for garbage collection by the time method g() is invoked?

output: information is insufficient to predict the result.

please explain.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Two.


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
You can't tell for sure. The object referenced to by c1 originally might not be eligible for garbage collection, because it might have been assigned to a different reference inside f(). Also, we can't tell if there are additional objects that could be garbage collected inside MyClass without knowing the structure of the class.


All code in my posts, unless a source is explicitly mentioned, is my own.
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

well as such you cannot be sure how many objects are available for GC with the given data, unless we know what the methods f and g are.


-kolli
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: garbage collection