This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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 8 in Action this week in the Java 8 forum!
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: 813

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection
 
Similar Threads
Garbage Collection in disguise
Another Garbage collection
Number of Objects eligible for Garbage Collection
How many object are eligible?
Stuck in garbage collection???????????