Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
vini singh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic