Difference between revisions of "Garbage Collection"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 7: Line 7:
 
== Ways in which Objects can be GC'ed ==
 
== Ways in which Objects can be GC'ed ==
  
* Nulling a reference.
+
* Nulling a reference - Explicitly assigning a reference to null will make it eligible for GC.
* Reassigning a reference.
+
* Reassigning a reference - Reassigning the reference to another object will cause older object to be eligible for GC.
* Isolating a reference.
+
** Local variables in a method will be eligible for GC when the method returns.
 +
* Isolating a reference - Even if objects have valid references, but still there is no way to reach them, they will be GC'ed.

Revision as of 04:52, 15 August 2011

Introduction

  • All GC involves in making sure the heap doesnt run out of space.
  • Purpose of the GC is to find and delete objects that cannot be reached.
  • An object is eligible to be garbage collected when no live thread can access it.

Ways in which Objects can be GC'ed

  • Nulling a reference - Explicitly assigning a reference to null will make it eligible for GC.
  • Reassigning a reference - Reassigning the reference to another object will cause older object to be eligible for GC.
    • Local variables in a method will be eligible for GC when the method returns.
  • Isolating a reference - Even if objects have valid references, but still there is no way to reach them, they will be GC'ed.