Need help Fix weak links in my application

I am trying to resolve a memory leak in my application. I downloaded and ran RedGate ANTS Memory Profiler 5.0, and the memory profiler tells me that the leak is related to WeakReferences.

The problem I am facing is that I never heard of WeakReference and was not explicitly declared in my application. From the reading I did, I believe that a weak link is created when you have an object / resource that is trying to be destroyed, but cannot, because too many other objects continue to reference it. I accept the same that the file cannot be deleted since it is still in use.

So my question is: how to determine where these weak links come from? I have a suspicion that this might be using ByRef? Another college suggested hash tables.

Hoping to get some clarification on identifying and eliminating weak links and some clarification on my suspicions.

Thanks.

+6
weak-references
source share
4 answers

I found that my WeakReferences were created by the System.Diagnostics.TextWriterTraceListener class. I still do not allow a memory leak, and I am so deep in it. I am beginning to doubt whether I have a memory leak or not, but I am relieved to know where WeakReferences comes from.

Thanks to everyone who posted!

+1
source share

In battles with .NET resource leaks (memory / descriptors / threads / etc.), we found one culprit, which is above all higher: delaying event handlers. If I have an object that I would like to delete, but I still have an event handler registered for the events of this object, then the object will not disappear - these zombies multiply and are built together in chains until boom! your managed application has a resource leak for all purposes and tasks.

We took the shotgun approach and broke our most used and heaviest classes for events, which we add both manually and using the VB.NET keyword β€œHandles” and ensure that RemoveHandler / - = Dispose is called for each of them. We are also clearly destroying as much as possible.

ANTS is a great tool to keep track of this, but it is not a simple tool (but it is the simplest tool I have found for these problems). Take some time to familiarize yourself with it, and if you are on version 5, use these new filters.

Unfortunately, there is no silver bullet, this is one of my biggest hype with .NET, because usually you do not know that you have a problem until it becomes terribly widespread and difficult to reign.

+3
source share

Using WeakReference should not cause a memory leak. This will cause the object to be assembled, but you still have access to it at the last minute.

I did not work with the memory profiler, but:

All my memory leak in .Net application is related to events / delegates.

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

If all other references to the object containing this method disappear, you might think that it will be assembled, but there is still a (invisible) link for the event.

+2
source share

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 functions. The only solution to this problem is to recompile in Release mode. (Unfortunately, if you used debug mode to provide detailed exception reports in production environments.)

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

Moataz Computer Engineer

+1
source share

All Articles