views:

364

answers:

4

Hello. I am trying to resolve a memory leak(s) in my app. I've downloaded and ran RedGate's ANTS Memory Profiler 5.0 and the memory profiler tells me the leak has to do with WeakReferences.

The problem I am running into is that I've never heard of a WeakReference nor are they explicitly declared in my app. From the reading I've done I believe weak reference's are created when you have an object/resource that is trying to be destroyed but cant because too many other objects keep referencing it. I assume much the same way that a file can't be deleted because it is still in use.

So my question is how do I determine where these weak references are coming from? I have suspicions that it might be the use of ByRef? Another collegue suggested hashtables.

Hoping to get some clarification on weakreference detection and elimination and some clarification on my suspicions.

Thanks.

+1  A: 

Using a WeakReference should not cause a memory leak. It would cause an object to be collected, but you still have a last minute option to access it.

I have not worked the the Memory profiler but:

All my memory leaks in .Net application have to do with events/delegates.

When you add a method to listen to an event, you create a reference from the object that contains the event (keyword) to the object that has the method you want to call.

If all other references to the object that contains the method are gone, you might think it will be collected, but there is still an (invisible) reference via the event.

GvS
Hi GVS. The one distinction I wanted to point out was that I'm not using WeakReferences...at least not explicitly anyways... What I'm seeing when I use a memory profiler on my app is that I have a lot of them popping up or being created indirectly and I'm not sure why.
M Murphy
The .Net framework itself uses WeakReferences internally, nothing you can do about that. I don't think the problem is the Weakreference itself, but the way you use some component, that is using a Weakreference. I still suspect delegates / events to be the root cause of your problems.
GvS
+2  A: 

In the battles with .NET resource leaks (memory/handles/threads/etc) we've found one culprit that rises above all else: lingering event handlers. If I have an object I'd like to dispose of, but I still have an event handler registered to an event of that object, then the object won't really go away--these zombies multiply and string together in chains until boom! you're managed app has what is for all intents and purposes a resource leak.

We've taken the shotgun approach and scour our most heavily used, and heaviest, classes for events we add both manually and by means of VB.NET's "Handles" keyword and ensure that RemoveHandler / -= is called for each during Dispose. We also explicitly Dispose of as much as possible.

ANTS is a great tool for tracking these down, but it's not a simple tool (but it is the simplest of the tools I've found for these issues). Spend some time getting familiar with it, and if you're on version 5 then make use of those new filters.

Unfortunately there's no silver bullet, it's one of my largest aggrivations with .NET because typically you don't know you have a problem until it's terribly wide-spread and hard to reign in.

STW
Thanks Yoooder for your wisdom. A couple things to note is that my app is really being implemented using a windows service and will be running constantly and the objects that use AddHandler will stay alive as long as the service is running so I dont have an opportunity to use RemoveHandler.We tried implementing IDisposable interface on all our classes but ended up with unforseen results and have had to roll back those changes.
M Murphy
+1  A: 

I discovered my WeakReferences were being created by the System.Diagnostics.TextWriterTraceListener class. I still havent resolved my memory leak and I'm so deep in it I'm starting to question whether I have a memory leak or not but I'm relieved to know where the WeakReferences are coming from.

Thanks to all who posted!

M Murphy
+1  A: 

This memory leak is a known bug in the .NET frameworks. __ENCList is an internal .NET class that is used to provide Edit and Continue functionality. The only resolution to this issue is to recompile in Release mode. (Which is unfortunate if you had been using the debug mode to provide detailed exception reporting in production environments.)

http://support.microsoft.com/?kbid=919481

Moataz Computer Engineer

interesting... I'm not sure what state we compiled in but I'll check it out now. Thanks!
M Murphy