my dog learned polymorphism
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic

garbage collection

vini singh

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();

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


Regards, Sud.
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.

I agree. Here's the link:
subject: garbage collection
It's not a secret anymore!