Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7765459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:10:17+00:00 2026-06-01T15:10:17+00:00

Here and there people keep talking about memory leaks which occur due unreleased event

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:10:18+00:00Added an answer on June 1, 2026 at 3:10 pm

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've heard people talking about base 64 encoding here and there. What is it
I've been reading things here and there for a while now about using an
I had a similar question answered Here There is a slight twist to the
i'm trying wmd editor over ajax. here there is the bugged code wdm code
I couldn't find a more descriptive title, but here there is an example: import
Simple question here: is there any way to convert from a jagged array to
I see there is a question here but there is no definite answer. Has
a simple question here Is there a way to change the text click here
I've heard bits and pieces here and there, but I have yet find a
I am a college student that makes his programs here and there in C#,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.