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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:03:40+00:00 2026-06-13T05:03:40+00:00

My code is using many Dictionaries and I’m having issues freeing memory from all

  • 0

My code is using many Dictionaries and I’m having issues freeing memory from all of them. When I investigate using what I learned in this answer I see that the GenericEqualityComparer is there, and I suspect that is what is keeping this memory in use.

Can anyone confirm this, or tell me how I can free up this memory?

Code

        Console.WriteLine("{0,10}: Start Point", GC.GetTotalMemory(true));
        List<string> t1 = new List<string>();
        Console.WriteLine("{0,10}: <------- Create List", GC.GetTotalMemory(true));
        t1 = null;
        Console.WriteLine("{0,10}: <------- null List", GC.GetTotalMemory(true));

        GC.Collect();
        Console.WriteLine("{0,10}: After GC.Collect", GC.GetTotalMemory(true));
        Dictionary<string, string> t2 = new Dictionary<string, string>();
        Console.WriteLine("{0,10}: <------- Create Dict", GC.GetTotalMemory(true));
        t2 = null;
        Console.WriteLine("{0,10}: <------- null Dict", GC.GetTotalMemory(true));


        GC.Collect();
        Console.WriteLine("{0,10}: After GC.Collect", GC.GetTotalMemory(true));
        Dictionary<string, string> t3 = new Dictionary<string, string>();
        Console.WriteLine("{0,10}: <------- Create Dict", GC.GetTotalMemory(true));
        t3 = null;
        Console.WriteLine("{0,10}: <------- null Dict", GC.GetTotalMemory(true));

        GC.Collect();
        Console.WriteLine("{0,10}: After GC.Collect", GC.GetTotalMemory(true));
        Dictionary<string, string> t4 = new Dictionary<string, string>();
        Console.WriteLine("{0,10}: <------- Create Dict", GC.GetTotalMemory(true));
        t4 = null;
        Console.WriteLine("{0,10}: <------- null Dict", GC.GetTotalMemory(true));

        GC.Collect();
        Console.WriteLine("{0,10}: After GC.Collect", GC.GetTotalMemory(true));
        Dictionary<string, string> t5 = new Dictionary<string, string>();
        Console.WriteLine("{0,10}: <------- Create Dict", GC.GetTotalMemory(true));
        t5 = null;
        Console.WriteLine("{0,10}: <------- null Dict", GC.GetTotalMemory(true));

        GC.Collect();
        Console.WriteLine("{0,10}: After GC.Collect", GC.GetTotalMemory(true));
        Dictionary<string, string> t6 = new Dictionary<string, string>();
        Console.WriteLine("{0,10}: <------- Create Dict", GC.GetTotalMemory(true));
        GC.KeepAlive(t6);
        t6 = null;
        Console.WriteLine("{0,10}: <------- null Dict", GC.GetTotalMemory(true));

        GC.Collect();
        Console.WriteLine("{0,10}: <------- End.", GC.GetTotalMemory(true));

Note: I have two versions of this code. One edit with GC.Collect as was suggested here on SO, but that affords no benefit.

Output without GC Collection

 95884: Start Point
 97872: <------- Create List
 97888: <------- null List
 97952: <------- Create Dict
 97968: <------- null Dict
 98032: <------- Create Dict
 98048: <------- null Dict
 98112: <------- Create Dict
 98128: <------- null Dict
 98192: <------- Create Dict
 98208: <------- null Dict
 98272: <------- Create Dict
 98288: <------- null Dict

Output With GC collection (per suggestion)

 96004: Start Point
 97992: <------- Create List
 98008: <------- null List
 98024: After GC.Collect
 98088: <------- Create Dict
 98104: <------- null Dict
 98120: After GC.Collect
 98184: <------- Create Dict
 98200: <------- null Dict
 98216: After GC.Collect
 98280: <------- Create Dict
 98296: <------- null Dict
 98312: After GC.Collect
 98376: <------- Create Dict
 98392: <------- null Dict
 98408: After GC.Collect
 98472: <------- Create Dict
 98488: <------- null Dict
 98504: <------- End.

Output in Release mode (per suggestion)

 96028: Start Point
 98016: <------- Create List
 98032: <------- null List
 98048: After GC.Collect
 98112: <------- Create Dict
 98128: <------- null Dict
 98144: After GC.Collect
 98208: <------- Create Dict
 98224: <------- null Dict
 98240: After GC.Collect
 98304: <------- Create Dict
 98320: <------- null Dict
 98336: After GC.Collect
 98400: <------- Create Dict
 98416: <------- null Dict
 98432: After GC.Collect
 98496: <------- Create Dict
 98512: <------- null Dict
 98528: <------- End.

Output in Release mode without GC Collect (per suggestion)

 96028: Start Point
 98016: <------- Create List
 98032: <------- null List
 98096: <------- Create Dict
 98112: <------- null Dict
 98176: <------- Create Dict
 98192: <------- null Dict
 98256: <------- Create Dict
 98272: <------- null Dict
 98336: <------- Create Dict
 98352: <------- null Dict
 98416: <------- Create Dict
 98432: <------- null Dict
 98448: <------- End.
  • 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-13T05:03:41+00:00Added an answer on June 13, 2026 at 5:03 am

    In debug mode GC does not collect objects in your current method scope, because references to them still exist. Try build it for release, run without debugging and check results.

    Code that prints true in debug mode but false in release:

    object obj = new object();
    WeakReference reference = new WeakReference(obj);
    GC.Collect(0, GCCollectionMode.Forced);
    Console.WriteLine(reference.IsAlive);
    

    Edit from OP: Debug mode did make the memory usage increase artificially. When I ran the stand alone app, I got this output. Thank you, and I’m accepting this answer.

     21852: Start Point
     29328: <------- Create List
     29328: <------- null List
     29328: <------- Create Dict
     29328: <------- null Dict
     29328: <------- Create Dict
     29328: <------- null Dict
     29328: <------- Create Dict
     29328: <------- null Dict
     29328: <------- Create Dict
     29328: <------- null Dict
     29376: <------- Create Dict
     29328: <------- null Dict
     29328: <------- End.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In many places in our application we have code like this: using(RAPI rapi =
I'm using c#. In many cases I'm writing code that can benefit from a
I need to map a many-to-many relationship using Entity Framework Code First. Its a
I add many check boxes to the excel sheet programaticaly using the following code:
My PHP code is split between many files, and often I find myself using
This code using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using System.Data.Entity.ModelConfiguration;
This is my code: @using (Html.BeginForm(UploadImages, Administration, new { _id = Model.Album.AlbumID, enctype =
I'm struggling with creating a simple one-to-many relationship using Code First in EF. I
I guess many people already read this article: Using your own SQLite database in
I'm compiling a low level code using many bunch of bytes. In some case

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.