For the following article, I do not go into the details of how to implement a Dispose() or Finalize() method. I am focused on why you would chose to use one over the other. I will say that you should rarely implement your own Finalize() (aka destructor) method. If you have any code cleanup, you should routinely perform that within the Dispose() method.
Also, though I use the term "Finalize() method", the method is NOT named "Finalize()". It is named "~<ClassName>()", kind of like a Constructor, but with a tilde ("~") preceding the method name.
---------------
Both Finalize() and Dispose() have a hand in the "death" of an object, but they have different intents. Dispose() is called by the code about the time that the object is no longer needed. Finalize() is called by the Garbage Collector at the time when the object is physically removed from memory.
An analogy:
Consider Dispose() to be when a loved one dies. Finalize() is the point where the body is buried in the ground (or cremated or buried at sea... pick your poison).
Dispose() and "Passing on":
Think of a time that a close relative or friend has passed away. What happens? Of course, there is mourning and sadness. Then, after a time has passed (hours, maybe a day), there are other tasks which must occur: 1) Call friends and relatives of the dearly departed person; 2) Make burial arrangements; 3) Have the funeral home prepare the body for burial; etc...
Similarly, Dispose() is called at the point that an object is no longer to be used. Most, if not all, of your object destruction logic should be housed here. The Dispose() method can still make decisions and execute statements. Dispose() can tell another object to do something.
Finalize() and Burial:
Again, considering the passing of a close relative or friend. On the day of burial, it is a day of extreme sadness. The focus of the day, however, is to lay the loved one to rest. Nothing more, really. No serious decisions usually remain, as they have already been made.
Similarly, the Finalize() method, if needed, shouldn't make any serious decisions. In fact, let's consider the various forms of burial: normal burial in a casket, cremation, and burial at see. The default Finalize() method would represent the normal burial in a casket. Only when there is a special request, would a custom Finalize() method be created. That Finalize() method would determine whether the body is cremated (and what to do with the ashes) or whether the body is to be buried at sea.
This brings to light another similarity between Finalize() and real burial. In the .Net Framework, the Garbage Collector has to already know that an object has a Finalize() method and will move that object to a different memory structure whose sole purpose is to process Finalize() methods before the object is truly removed. This also means that the object will pass through the Garbage Collector at least twice.
------------------
I hope that I have shed a little bit of light on the differences between Dispose() and Finalize(). I encourage you to research these, further, to learn the implementation details of each.
CD
No comments:
Post a Comment