Dima
Dima

Reputation: 1761

Example of memory leak in .NET due to event handlers please?

Here and there people keep talking about memory leaks which occur due unreleased event listeners. I think this is very important problem. Very serious and very important... if it really exists.

I have tried myself to reproduce the problem but all my attempts failed: I just can not make my application leak memory :( While it sounds good, I am still worried: maybe I am missing something.

So maybe can somebody provide a very simple source code sample which causes memory leaks?

I have created a small VB.NET application as a demo: it contains one Windows form and one class.

Windows form: it has a collection object (named "c") adn two buttons: one to add 10 items to collection and another one to clear the collection:

Public Class Form1

Dim c As New Collection

Private Sub btnAddItem_Click(sender As System.Object, e As System.EventArgs) Handles btnAddItem.Click
    For i As Integer = 1 To 10
        Dim m As New MyType
        c.Add(m)
    Next

    Me.Text = c.Count
End Sub

Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
    For Each item As MyType In c
        item.Dispose()
    Next
    c.Clear()

    Me.Text = c.Count
End Sub
End Class

MyType class: it has big m_Image object, which is big so you can see your memory is really taken by MyType instances :)

Imports System.Drawing

Public Class MyType 
Implements IDisposable

Private m_Image As Bitmap

Public Sub New()
    AddHandler Application.Idle, AddressOf Application_Idle

    m_Image = New Bitmap(1024, 1024)
End Sub

Private Sub Application_Idle(sender As Object, e As EventArgs)

End Sub

#Region "IDisposable Support"
Private disposedValue As Boolean

Protected Overridable Sub Dispose(disposing As Boolean)
    If Not Me.disposedValue Then
        If disposing Then
            m_Image.Dispose()
        End If
    End If
    Me.disposedValue = True
End Sub

Public Sub Dispose() Implements IDisposable.Dispose
    Dispose(True)
    GC.SuppressFinalize(Me)
End Sub
#End Region

End Class

Upvotes: 6

Views: 2678

Answers (3)

supercat
supercat

Reputation: 81179

The most common pattern by which an event becomes a memory (and CPU time!) leak occurs when an object asks another object for notification that something has happened, so that it can update some information which is interest only to short-lived objects. If the event subscription continues to exist even all the objects that used to care about it have been abandoned, memory will be wasted as long as the object doing the notifying continues to exist, and CPU time will be wasted every time the object performs the notification. If an unbounded number of such event subscriptions can be created and abandoned, they will constitute an unbounded memory leak.

Upvotes: 0

Dima
Dima

Reputation: 1761

After more investigation (thanks to clues from @JaredPar), I found out that memory leak occurs when we have such conditions:

  1. Create reference REF1 to new object which has public procedure or function (for example procedure PRC1()).
  2. From any place in your code add event handler: link any event (for example EVNT1) to PRC1 procedure in REF1 object.
  3. Remove REF1 (set it to null or Nothing in VB). From this moment you have no references to the object you have created in step 1.
  4. However object stays in memory, since it is logical: it possesses a code (PRC1) which is executed when event fires (EVNT1).

While I don't give you any advice how to free your memory in such situation, I hope this description will help you design better architecture and avoid memory leaks.

Upvotes: 0

JaredPar
JaredPar

Reputation: 754813

Here is a very straight forward example

class MyType
{
    public static event EventHandler ExampleEvent;

    public MyType()
    {
        ExampleEvent += (sender, e) => OnExampleEvent();
    }
    private void OnExampleEvent() { }
}

Any instance of MyType will subscribe to the ExampleEvent event. This event isn't attached to any specific object hence it will never leave memory. This will hold all instances of MyType in memory for the duration of the application.

EDIT

As asked for in the comments here is a demo of the MyType instance staying in memory long after it's no longer used

class Program
{
    static void Main(string[] args)
    {
        WeakReference weakRef = new WeakReference(new MyType());
        for (var i = 0; i < 10; i++)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        Console.WriteLine("Still Alive: {0}", weakRef.IsAlive);
    }
}

Upvotes: 12

Related Questions