Karl Seguin

Sponsors

The Lounge

Wicked Cool Jobs

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Foundations of Programming - pt 7 - Addendum

I've made two additions to Part 7. The first is based on a suggestion by Greg to talk about a common cause of memory leaks - events and delegates. The second is about deterministic finalization

Memory Leaks with Events
There's one specific situation worth mentioning as a common cause of memory leaks: events. If, in a class, you register for an event, a reference is created to your class. Unless you de-register from the event your objects lifecycle will ultimately be determined by the event source. In other words, if ClassA (the listener) registers for an event in ClassB (the event source) a reference is created from ClassB to ClassA. Two solutions exists: de-registering from events when you're done (the IDisposable pattern is the ideal solution), or use the WeakEvent Pattern or a simplified version.

 


Deterministic Finalization

Despite the presence of the garbage collector, developers must still take care of managing some of their references. That's because some objects hold on to vital or limited resources, such as file handles or database connections which should be released as soon as possible. This is problematic  since we don't know when the garbage collector will actually run - by nature the garbage collector only runs when memory is in short supply. To compensate, classes which hold on to such resources should make use of the Disposable pattern. All .NET developers are likely familiar with this pattern, along with its actual implementation (the IDisposable interface), so we won't rehash what you already know. With respect to this chapter, it's simply important that you understand the role deterministic finalization takes. It doesn't free the memory used by the object. It releases resources. In the case of database connections for example, it releases the connection back to the pool in order to be reused.

If you forget to call Dispose on an object which implements IDisposable the garbage collector will do it for you (eventually). You shouldn't rely on this behavior however as the problem of limited resources is very real (it's relatively trivial to try it out with a loop that opens connections to a database). You may be wondering why some objects expose both a Close and Dispose method, and which you should call. In all the cases I've seen the two are generally equivalent - so it's really a matter of taste. I would suggest that you take advantage of the using statement and forget about Close. Personally I find it frustrating (and inconsistent) that both are exposed.


Finally, if you're building a class that would benefit from deterministic finalization you'll find that implementing the IDisposable pattern is simple. A straightforward guide is available on MSDN.


Posted Sun, May 4 2008 4:28 PM by karl
Filed under:

[Advertisement]

Comments

John Spurlock wrote re: Foundations of Programming - pt 7 - Addendum
on Sun, May 4 2008 10:07 PM

"If you forget to call Dispose on an object which implements IDisposable the garbage collector will do it for you (eventually)."

This is common source of confusion.  The GC knows neither about the IDisposable interface nor the Dispose() method.  It will never call Dispose(), only the finalizer.

It is typical (and suggested by MS) to call a common method in both Dispose() and the finalizer but this is by no means required - and certainly not always followed.

karl wrote re: Foundations of Programming - pt 7 - Addendum
on Mon, May 5 2008 8:23 AM

You're right John :)

Dew Drop - May 5, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - May 5, 2008 | Alvin Ashcraft's Morning Dew
on Mon, May 5 2008 8:32 AM

Pingback from  Dew Drop - May 5, 2008 | Alvin Ashcraft's Morning Dew

Reflective Perspective - Chris Alcock » The Morning Brew #87 wrote Reflective Perspective - Chris Alcock » The Morning Brew #87
on Tue, May 6 2008 3:23 AM

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #87

Dave Mellors wrote Learning how to program - again!
on Sun, Jun 22 2008 12:00 PM

The problem with not programming all the time is keeping up with the constant developments in programming

Devlicio.us