4

I have an idea about Dispose and Finalize method in .Net as laid out below. Is this correct?

Dispose : Implement IDisposable interface and remove unused/unmanaged code in the Dispose() method. The developer needs to call it manually if they want immediate removal or GC will dispose the resources when it is invoked.

Finalize : When GC invoked it will free the unused managed code and if IDisposable is implemented then it will call the Dispose() method to free up the unmanaged resources (normally).

Essentially, when we dispose the resources using the Dispose() method, will memory will be freed immediately and compacted (like the GC does)?

TarHalda
  • 1,050
  • 1
  • 9
  • 27
Syed
  • 931
  • 13
  • 28

3 Answers3

5

The answer to your question is no: releasing the memory allocated for the object has nothing to do with calling the Dispose method. It happens in due course when the garbage collector gets to it.

Generally speaking, Dispose is intended for speeding up the release of external resources, such as file handles, semaphores, db handles, and other items often allocated by the operating system. If your object holds on to other IDisposable objects, it should dispose them in its call to dispose as well.

Finalizer, however, is different: it is called as part of garbage collection, and is intended for releasing external resources that have not been released during the dispose (presumably, because the user forgot to call Dispose). Finalizers must not call Dispose of other objects that your object may hold, because they are in the process of being garbage collected already.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • When the system determines that it is necessary to run the finalizer on an object, it and all other objects to which it holds references will be temporarily resurrected, and can thus not be garbage-collected until after the finalizer has run. It is probable that when a finalizer runs on an object, any other objects to which it holds references will either (1) already have run their finalizer; (2) have their finalizer scheduled to run; or (3) be in use by something else. In none of those cases should one run Dispose on the other object, but that doesn't mean the other object is "being GC'ed". – supercat Feb 09 '12 at 03:45
  • @supercat that's why I did not say "have been garbage collected already" - I meant that the dependents are somewhere "in the pipeline" of the garbage collector, perhaps even finalized; calling `Dispose` on these objects is a bad idea (I was unfortunate to learn it the hard way). – Sergey Kalinichenko Feb 09 '12 at 03:55
  • I consider finalization separate from garbage collection, since an object which has been garbage-collected doesn't exist anymore. If one were to hide the only existing reference to an object from a GC (e.g. by using unsafe code to convert it to an int, and then later turn it back into an object reference), attempting to access the reconstituted object reference would be equivalent to a stray pointer reference in C. By constrast, any object references held by objects that are being finalized will refer to objects that, as far as the .net GC is concerned, still exist. – supercat Feb 09 '12 at 06:54
2

No. Calling the Dispose method directly or via a using statement will not cause memory to be freed.

Implementing IDisposable will just give your class a chance to clean up any unmanaged resources its holding onto.

Andrew Kennan
  • 13,947
  • 3
  • 24
  • 33
1

Finalize : When GC invoked it will free the unused managed code and if IDisposable is implemented then it will call Dispose method to free up the unmanaged resources(normally).

You are somewhat incorrect here. When you say "it will call Dispose", if you are referring to the GC itself, then, no, it does not "automatically" call Dispose for you. It is your job as the programmer to do cleanup in both the Dispose and Finalizer methods.

This MSDN writeup here demonstrates the typical disposal pattern.

The question is: When we dispose the resources using Dispose method, memory will be freed immediately and compacted(as GC doing)?

No, calling Dispose does not free heap memory. The heap memory is not released until the GC runs and performs the cleanup.

CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138