3

When is any particular object eligible for garbage collection? And which are the parameters that comes into consideration which deciding this?

MyClass mc = new MyClass();
mc.myMethod(); //line1

mc=null; //line2

mc=new object(); //line3
mc.myMethod(); //line4

mc=null; //line5

In this case, when mc object is eligible for garbage collection?

user207421
  • 305,947
  • 44
  • 307
  • 483
Raghvendra
  • 101
  • 1
  • 2
  • 5
  • at line 2 object created at line 1 and at line 5 object created at line 3, are eligible for GC. Unless any new threads are spawn in myMethod() that uses these objects – Renjith Oct 01 '14 at 08:41
  • What is mc object???? The MyClass object which is created at line 1 may be eligible for garbage after line 2 is executed. The object which is created at line 3 may be eligible for garbage after line5 is executed. – Fumu 7 Oct 01 '14 at 08:43

3 Answers3

1

This decission is taken by the garbage collector itself, the condition for an object to be collected is made upon the number of references to it. So if an object does not have any references it becomes candidate to collection.

That does not mean that it is inmediatelly collected, just a candidate to collection. The object collection is governed by the generations mechanism:

An object is considered garbage when it can no longer be reached from any pointer in the running program. The most straightforward garbage collection algorithms simply iterate over every reachable object. Any objects left over are then considered garbage. The time this approach takes is proportional to the number of live objects, which is prohibitive for large applications maintaining lots of live data.

Concerning your code, the object created in the first line MyClass mc = new MyClass(); becomes a candidate to collection right after the execution of the third one mc=null; //line2

Following the same pattern, the new object created at line3 (I don't think however that line makes what you think it does: Shouldn't that line be mc = new MyClass();?) becomes a candidate after the execution of line 5.

Note that even though you remove line2, the first object will be elegible for collection because it is not referenced any longer.

0

As soon as there are no references from active threads (directly or indirectly through things like static fields) to an object instance anymore, it is eligible for garbage collection.

In your case, after you null out the only reference to your objects in lines 2 and 5 respectively (you have two objects after all).

Of course, if mc.myMethod() has a weird side effect that keeps mc alive (such as saving it in a thread local or a static variable), then this may not be the case.

Thilo
  • 257,207
  • 101
  • 511
  • 656
0

Every object that has not a variable pointing to it becomes eligible for garbage collection.

When you do: mc=null; //line2

The first object you created becomes eligible for Garbage collection because the variable mc is no longer pointing to it!

This line won't compile: mc=new object();

You can't create an object in this way. What's object?

Alboz
  • 1,833
  • 20
  • 29