Sep 21 2004

Yes, the GC *can* leak memory – there I said it!

Category: .architectureAmit Bahree @ 4:21 am

Shawn Van ness has an excellent article, that spells out how event listeners can cause memory leaks, yep even when running in managed code. Steve Main sums it up pretty well:

The main issue is the “lapsed listener” problem. This occurs when objects subscribe to events and subsequently get out of scope. The problem is that the event subscriber doesn’t get garbage collected because the event is still holding a reference to it inside of the event’s invocation list. The event subscriber is still considered reachable from the GC’s point of view. As such, it doesn’t get collected until the event goes out of scope (which is usually at application shutdown) which means that the event subscriber is effectively “leaked”.

Moral of the story: when you implement an Observer pattern, it’s important to consider the relative lifetime of events and subscribers. If implemented naively, you’ll end up having objects that live a lot longer than you think they should. Unsubscribe() is your friend.

As Fabrice writes, .NET’s delegates and events are implementations of the Observer Design Pattern . But the current problem is one more reminder that Design Patterns should not be applied blindly.

If you write the following code, you’ll see that the object instance gets correctly released and collected:

StoopidObject object = new StoopidObject();
GC.Collect();
GC.WaitForPendingFinalizers();
 
If you write the following code instead, although there is no apparent reference kept to the Observer, the Observer instance will not be released:
 
Observer observer = new Observer();
Subject subject = new Subject();
subject.SomethingHappened += new EventHandler(observer.subject_SomethingHappened);
GC.Collect();
GC.WaitForPendingFinalizers();

 

Guys from around the community came with various solutions. They call them Weak Delegates. Follow the links to learn more:

More Information:

Share
Similar posts to check out:
  • June 16, 2011 -- Occasionally Connected Architecture (0)
    When implementing an occasionally connected architecture for a solution, there are three fundamental requirements: Part of the overall solution, some smart client is deployed and installed on the desktop and a web only approach is not possible. The main rational being that a smart client can work in a disconnected mode which of course with a web application is not possible. Underlying infrastructure needs to be in place to support this. Infrastructure is not specifically networks and ser...
  • December 29, 2010 -- C++ Message queuing options? (1)
    I am thinking of implementing a queue in one of the projects I am working on right now (sorry cannot go into more details until it gets published - hopefully in a few months). Anywyas, this is in C++ which needs to run on Ubuntu and my queueing experience (with C++ or otherwise) is only with MSMQ which is brilliant, but does not help me here as that run only on Windows. I also cannot use something like STL Queue as this will need to run across a number of machines and trying to sync between them...
  • November 6, 2010 -- MOOS (0)
    I don't think many people have heard of MOOS (which stands of Mission Oriented Operating Suite); I have been working with it for the past few months as part of my dissertation. And I must admit, the more I play with it, the more impressed I am. It is quite simple and yet powerful. Whilst MOOS's roots are in robotics (MRG) and embedded systems, I wonder if I can extend it to use it some of the grid computing scenarios. Maybe implement a pMapReduce or pHadoop? Or perhaps a .NET implementation. ...

Tags:

Leave a Reply

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes