Undisposed Instances - EventHandlerList on Snapshot

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
Post Reply
tdjohn
Posts: 1
Joined: Tue Mar 03, 2009 9:02 am

Undisposed Instances - EventHandlerList on Snapshot

Post by tdjohn » Tue Mar 03, 2009 9:21 am

Hi

I've been using the profiler to try and find the cause of excessive memory usage in our .Net Windows Forms application.

Currently I am focusing on System.ComponentModel.EventHandlerList. When monitoring Disposed Info in real time, there are a few undisposed instances of the object, however when a snapshot is taken, this number jumps to about 1000.

Is this caused by the profiler taking the snapshot, or is this an indication of a potential problem in our code?

Thanks

Tom

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Post by Andreas Suurkuusk » Tue Mar 03, 2009 8:26 pm

An instance is not counted as undisposed until it has been garbage collected (or finalized). When you run your application, you can have several EventHandlerList instances that are no longer in use, but have not yet been garbage collected (they might have been promoted to a higher generation). When a snapshot is collected, multiple garbage collections will also be performed. These GCs are performed to make sure that all "unused" instances are properly garbage collected. As soon as the EventHandlerList instances are GCed, they will also be counted as undisposed (unless they were properly disposed).

It's pretty common that EventHandlerList instances are undisposed in the framework. The EventHandlerList dispose method only clears a reference, so the undisposed instances will not cause any side effects.
Best regards,

Andreas Suurkuusk
SciTech Software AB

mikej
Posts: 5
Joined: Wed Apr 30, 2008 8:09 pm

clarification

Post by mikej » Tue May 05, 2009 8:58 pm

I am seeing a very similar issue

I see UserPreferenceCahgedEventHandlers referencing items is my winform. They appear to be keeping many usercontrols from being disposed. This seems to be a problem and is causing me much grief as I try to get to the root cause of the problem.

I have found that if I call the Dispose method directly I can get things to clean up but I have seen 1 bad side affect from doing this.

Any insight on how to drill down and find the reference that is keeping things in memory would be very welcome.
Mike

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Post by Andreas Suurkuusk » Wed May 06, 2009 1:57 pm

The UserPreferencesChanged event is a static event. If event handlers are not correctly removed from it, then it's very likely that you will get a memory leak. You can get more information about why the event is added (and hopefully how it can removed) by investigating the call stack of the UserPreferencesEventHandler instance in the root path.

A while ago I wrote a blog post about a memory leak that involved the UserPreferencesChanged event. Maybe you can get some ideas by reading that post. The post can be found at:

http://www.scitech.se/blog/index.php/20 ... boxcontrol
Best regards,

Andreas Suurkuusk
SciTech Software AB

mikej
Posts: 5
Joined: Wed Apr 30, 2008 8:09 pm

What version of .NET Framework?

Post by mikej » Wed May 06, 2009 2:32 pm

I am reading the blog and wonder what version of the .NET Framework you were seeing this problem on?

I have seen the same issues in my application. I use the ToolStrip control. Much of the information on the internet documents a bug with the ToolStrip control & .NET Framework 1.1. I am using 2.0.
Mike

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Post by Andreas Suurkuusk » Wed May 06, 2009 7:49 pm

The memory leak described in the blog is based on .NET Framework 2.0, so if you have are using .NET 2.0 you will be affected by this. However, the last comment on the post says that this has now been fixed (according to the comment the issue was closed on 2009/02/03). I have not confirmed this yet though.
Best regards,

Andreas Suurkuusk
SciTech Software AB

mikej
Posts: 5
Joined: Wed Apr 30, 2008 8:09 pm

Fix was for ReportViewer

Post by mikej » Wed May 06, 2009 10:01 pm

It appears they fixed the ReportViewer control, but it does not appear they have fixed the underlying problem with the ToolStrip itself. I am pretty sure it is still there :-(

Let me clarify, I am using .NET Framework 2.0 SP1.
Mike

Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests