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 8372809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:31:47+00:00 2026-06-09T14:31:47+00:00

I have got a simple ConsoleProgram which is creating a list of 80 IDisposable

  • 0

I have got a simple ConsoleProgram which is creating a list of 80 IDisposable objects. This object hold two System.Threading.ManualResetEvent which are closed in the Dispose() method. Please consider the code:

public class Program
{
    private static void Main(string[] args)
    {
        Console.ReadLine();
        Test(80);
        Console.WriteLine("end.");
        Console.ReadLine();
    }

    private static void Test(int c)
    {
        Console.WriteLine("Test start.");

        List<TestObject> list = new List<TestObject>();

        for (int i = 0; i < c; i++)
            list.Add(new TestObject());

        Console.WriteLine("End of adding. Added: {0} items.", c);
        Console.ReadLine();

        foreach (TestObject obj in list)
            obj.Dispose();

        list = null;
        Console.WriteLine("Dispose end.");
    }

    public class TestObject : IDisposable
    {
        public ManualResetEvent mr1 = new ManualResetEvent(true);
        public ManualResetEvent mr2 = new ManualResetEvent(false);

        public void Dispose()
        {
            mr1.Close();
            mr2.Close();
        }
    }
}

I have tested my program in case of consumed memory and memory leaks. I supposed that consumed memory will increase after creating all list objects, but it will decrease after calling the Dispose() method and setting null value to the list object. Unfortunately I have observer different behaviour. Please consider following result of my test:

  1. Program starts (nothing created). WorkingSet = 6.700K
  2. List of 80 objects was created. WorkingSet = 7.160K (memory grow: 460K)
  3. Program called the Dispose() method an set null to list object. WorkingSet = 7.164K (memory grow form last point: 4K)
  4. Program is hanging for more than 20 minutes. WorkingSet = 7.296K (memory grow from last point: 105K, memory grow from the beginning: 596K)
  5. Program was closed.

I am really confused about the point 3 and 4. Why the memory was not released? I think that this is a memory leak, because the total memory grow was equal to 596K and it was never released.

Thank you very much for any answer.

PS. Changing the number of object, for example to 9000, cause a memory growth of more than 2MB.

PS2. At the end of my program I am calling GC.Collect() to force the clean-up. But after that amount of used memory is still the same and it is not decreasing – I am confused.

  • 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-09T14:31:49+00:00Added an answer on June 9, 2026 at 2:31 pm

    Calling Dispose() and setting a value to null does not free the memory up immediately. Your memory should be released when the Garbage Collector next runs, but this time is not deterministic. Typically, it will happen when the application is under memory pressure, usually as a result of object creation requesting more memory. Without that pressure, and with the application idle, the GC might never collect your memory.

    In short, this is not a memory leak.

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

Sidebar

Related Questions

I have got a ball which bounces of walls. This bounce is simple, i
I have a simple problem with XML got from database. Now, this XML can
I have got an simple html unordered list. <ul> <li>Item 1</li> <li> Group 1
I know that I am missing something simple here. I have got this homework
I have got two simple questions How can I tell what server is a
I have got a div object for example (Simple Image), and I need my
I have got an assignment in which I should program a simple teller machine
I have got a function, inside which are some simple expressions, adding nums, appending
I have got pretty simple list: example_list = [ {'points': 400, 'gold': 2480}, {'points':
I have got a simple table like this: <table> <tr> <td id=col1><p>test1</p></td> <td id=col2>

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.