6

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
2
  • It somewhat depends on your definition of a Memory Leak, and in what context. Are we talking about a web app, or desktop app? Apr 7, 2012 at 16:53
  • Well, if it makes a difference, it would be nice to see code samples for both app types: desktop and web.
    – Dima
    Apr 7, 2012 at 17:22

3 Answers 3

12

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);
    }
}
13
  • 1
    I'm not sure that's an example of a memory leak, since you are specifically subscribing to a static global event. That's a little like saying you have a memory leak because main doesn't exit until the application exits. Apr 7, 2012 at 16:50
  • 1
    @MystereMan the OP asked for an event based memory leak and this is an example of one. This could be repeated for any event which has a lifetime greater than the intended life time of the object which subscribes to the event.
    – JaredPar
    Apr 7, 2012 at 16:51
  • Thanks for quick reply, JaredPar. However I do not see the problem in your example. I edited my topic: added sample code (it's in VB). In my sample MyType subscribes to Application.Idle event, which is available during complete application lifetime (could not locate SystemEvents.TimeChanged event ;)).
    – Dima
    Apr 7, 2012 at 17:17
  • @Dima if your object is intended to live as long as the application then it's not a memory leak. A memory leak is generally defined by the object being held in memory longer than it's intended use. In that particular case it looks like it's intended to live that long and hence not a problem.
    – JaredPar
    Apr 7, 2012 at 17:21
  • Well, let's say my demo app's (demo from topic) high mission is to fill collection with dummy objects (MyType), and clear the collection. So I can press "Add" button 10 times and get a pack of objects in the collection. It will take 100MB for example. Then I can clear the collection. 100MB are released. I can create a pack of objects again. Fill 200MB. Clear collection again - release 200MB of memory. So the thing I can not uderstand is: where is the memory leak?
    – Dima
    Apr 7, 2012 at 17:28
0

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.

0

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.

1
  • Hi, general memory leak does not occur from simple usage of events. Specific bad design leads to memory leaks. Not events themselfs. You can see a description of bad design situtation in previous answers.
    – Dima
    May 5, 2012 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.