Raghav55
Raghav55

Reputation: 3135

garbage collection

if a object contains finalize method, but still the object is referenced, does the garbabe collector removes the object from the heap or it just cleans the unmanaged resources

Upvotes: 0

Views: 298

Answers (4)

Aliostad
Aliostad

Reputation: 81700

Finalize is called when an object is Garbage Collected. Simply speaking (although there are many exceptions), if all references to an object go out of scope, when GC wakes up (tons of conditions can trigger it but it usually memory thresholds) it will call the finalize and then reclaim the managed memory.

So if your object still has references that are in scope, it will not be garbage collected (again there are scenarios and exceptions).

GC will not care about unmanaged, but you do so you need to implement Dispose pattern by implementing IDisposible.

Upvotes: 0

Ryan Bennett
Ryan Bennett

Reputation: 3432

http://www.csharphelp.com/2006/08/garbage-collection/

Great article on garbage collection and finalize. Way too in depth for a full on answer here if you ask me.

Whenever a new object, having a Finalize method, is allocated on the heap a pointer to the object is placed inan internal data structure called Finalization queue. When an object is not reachable, the garbage collector considers the object garbage. The garbage collector scans the finalization queue looking for pointers to these objects. When a pointer is found, the pointer is removed from the finalization queue and appended to another internal data structure called Freachable queue, making the object no longer a part of the garbage. At this point, the garbage collector has finished identifying garbage. The garbage collector compacts the reclaimable memory and the special runtime thread empties the reachable queue, executing each object’s Finalize method.

The next time the garbage collector is invoked, it sees that the finalized objects are truly garbage and the memory for those objects is then, simply freed.

Thus when an object requires finalization, it dies, then lives (resurrects) and finally dies again.It is recommended to avoid using Finalize method, unless required.Finalize methods increase memory pressure by not letting the memory and the resources used by that object to be released, until two garbage collections. Since you do not have control on the order in which the finalize methods are executed, it may lead to unpredictable results.

Upvotes: 0

Vilx-
Vilx-

Reputation: 107000

If the object is still referenced, the GC has no reason to collect it or run the finalizer.

Also, GC does not clean up any unmanaged resources. It just reclaims the memory used by your objects. You have to clean up all your unmanaged resources yourself in your finalizer and, prefereable, IDisposable.Dispose().

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 191037

GC in .NET does not automatically clean up unmanaged resources. That's what IDisposable is for. The finalize won't be called until all references no longer needed.

Upvotes: 4

Related Questions