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:
  • April 18, 2012 -- Concurrency and CEP (0)
    The sooner we all understand the Concurrency ≠ CEP (Complex Event Processing), the better the world will be! CEP is generally used when we implement real-time systems (of course that is not the only area where CEP is used). Real-time does not mean concurrent or for that matter high-performing system. Of course there are correlations, but at the same time they are fundamentally different paradigms....
  • March 17, 2012 -- What I am working on today? Optimisation Algorithms (0)
    I often get the question – a what am I working on today? Some of the things I can’t discuss in an open forum, but some I can. Those that I can, I thought it was best to share via my blog and do quick small posts on it. Will this become a new series? Well time will tell – depends on how much bandwidth I will have. This weekend, I am researching Optimisation Algorithms – both Deterministic and Probabilistic. Specifically interested in Swarm Intelligence (which are a type of Monte Carlo algorithm)...
  • 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...

Tags:

Leave a Reply

*

Get Adobe Flash player