Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 779
Dima Avatar asked Apr 07 '12 16:04

Dima


1 Answers

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);
    }
}
like image 115
JaredPar Avatar answered Nov 15 '22 04:11

JaredPar