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

  • SEARCH
  • Home
  • 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 308599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:39:55+00:00 2026-05-12T07:39:55+00:00

I work on a project where there is a huge number of objects being

  • 0

I work on a project where there is a huge number of objects being instanced by a few classes that stay in memory for the lifetime of the application. There are a lot of memory leaks being caused with OutOfMemoryExceptions being thrown every now and again. It seems like after the instantiated objects ago out of scope, they are not being garbage collected.

I have isolated the problem to being mostly about the event handlers that are attached to the long-living object that are never detached, thus causing the long-living object to still have a reference to the out of scope objects, which then will never be garbage collected.

The solution that has been proposed by my colleagues is as follows: Implement IDisposable on all classes, across the board and in the Dispose method, null all the references in your objects and detach from all event that you attached to.

I believe this is a really really bad idea. Firstly because it’s ‘overkill’ since the problem can be mostly solved by fixing a few problem areas and secondly because the purpose of IDisposable is to release any unmanaged resources your objects control, not because you don’t trust the garbage collector. So far my arguments have fallen on deaf ears. How can I convince them that this is futile?

  • 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-05-12T07:39:55+00:00Added an answer on May 12, 2026 at 7:39 am

    By coincidence I just posted this comment elsewhere:

    An reference to an object being
    incorrectly retained is still a
    resource leak. This is why GC programs
    can still have leaks, usually due to
    the Observer pattern – the observer is
    on a list instead the observable and
    never gets taken off it. Ultimately, a
    remove is needed for every add, just
    as a delete is needed for every new.
    Exactly the same programming error,
    causing exactly the same problem. A
    “resource” is a really just a pair of
    functions that have to be called an
    equal number of times with
    corresponding arguments, and a
    “resource leak” is what happens when
    you fail to do that.

    And you say:

    the purpose of IDisposable is to release any
    Unmanaged resources your objects
    controls

    Now, the += and -= operators on an event are effectively a pair of functions that you have to call an equal number of times with corresponding arguments (the event/handler pair being the corresponding arguments).

    Therefore they constitute a resource. And as they are not dealt with (or “managed”) by the GC for you, it can be helpful to think of them as just another kind of unmanaged resource. As Jon Skeet points out in the comments, unmanaged usually has a specific meaning, but in the context of IDisposable I think it’s helpful to broaden it to include anything resource-like that has to be “torn down” after it has been “built up”.

    So event detaching is a very good candidate for handling with IDisposable.

    Of course, you need to call Dispose somewhere, and you don’t need to implement it on every single object (just those with event relationships that need management).

    Also, bear in mind that if a pair of objects are connected by an event, and you “cast them adrift”, by losing all references to them in all other objects, they don’t keep each other alive. GC doesn’t use reference counting. Once an object (or island of objects) is unreachable, it is up for being collected.

    You only have to worry about objects enlisted as event handlers with events on objects that live a long time. e.g. a static event such as AppDomain.UnhandledException, or events on your application’s main window.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I work on a project that uses multiple open source Java libraries. When upgrades
I've started work on a project that will be primarily acting as a Sync
I'm trying to work through Project Euler and I'm hitting a barrier on problem
I'm starting work on a project using Rails, but I'm waiting for the 3rd
I recently had to work on a project where the previous developer modified the
I've been doing a project at work recently focused on an almost entirely client-driven
Programming Student here...trying to work on a project but I'm stuck. The project is
I am starting a new client/server project at work and I want to start
I recently got involved in a Java project at work: we're using MyEclipse for
We are ready to start a brand new project at work, no legacy code.

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.