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:

Other similar posts to check out:
  • December 3, 2007 -- Good UML Tool (and free too) (0)
    Love it or hate it UML is important for anyone involved (Architect/Developer/Whoever) - either you need to create designs based on UML and you need to understand that someone else has. Sure it has its challenges and for some specific things there are better solutions (DSL's - more on that some other day). I am "old school" and over the years have used Rational Rose (or whatever IBM has renamed it to since buying Rational out). But as are aware Rational tools are very pricey and I ...
  • September 20, 2007 -- Rules of Threading (0)
    As Mr. Kale so eloquently puts it (and of course 100% correct): If you think you need multi-threading, you're wrong If your specification says "you need threading", see Rule 1 (For advanced users only) If you think you need multi-threading, you're probably wrong....
  • March 16, 2007 -- Architect Insight presentations available (0)
    I had the opportunity to present on .NET 3.0 in the Enterprise at the Architect Insight Conference hosted by Microsoft earlier this month. All the presentations from the conference are available for download and of course including my presentation (13.6 mb)....
  • February 15, 2007 -- Microsoft Architect Insight Conference (0)
    Microsoft hosts an annual event in the UK called the Architect Insight Conference. I am one of the speakers this year and will be presenting on ".NET 3.0 in the Enterprise". This is a pretty good event and I would recommend it if you have not been to one of these. You can check out the Agenda here and if you want, register here and you can find out more information on the Speakers here. Here is a blurb from Microsoft on what can you expect to hear: You’ll be able to engage in the tech...
  • June 14, 2005 -- One man's feature is another man's bloat – The Java Performance Debate (0)
    Firstly this is not a Java bashing and I don't preach to say .NET/C++, etc is faster. However, based on what I have seen it sure is slow - slow like a snail. Maybe its the time that takes to load the VM or maybe it Swing - gurk! I like how Andy puts it - "One man's feature is another man's bloat". He has a very objective article on the area which are slow, what Sun is doing to address it and what the main issues (with the developers) are - who don't know how to use it. He talks about the Memory,...

Tags:

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes