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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:36:07+00:00 2026-05-29T18:36:07+00:00

Possible Duplicate: Possible memoryleak in ConcurrentBag? I have epic memory leak in my app.

  • 0

Possible Duplicate:
Possible memoryleak in ConcurrentBag?

I have epic memory leak in my app. All data, that i add in local concurrentBag collection in one of methods, was never collected.

This simple code demonstrate, how I use it:

void Main()
{
    var l = new List<int>(){1,2,3,4};
    Func(l);
    l.Clear();
    l=null;
}

void Func(List<int> list)
{
    var bag = new ConcurrentBag<int>();
    Parallel.ForEach(list, k=> bag.Add(++k));

    list.Clear();

    foreach(int i in bag) //I know, I doing it wrong.
    {
        list.Add(i);
    }
}

What I expect: bag will be created and dispose at method “Func”.

What I see: bag is never dispose, hold all threads that created in Parallel.ForEach, hold all data, that I add in it. =(

Okay, I can use “TryTake” to remove item from bag, when I add it in list. But empty bag is still hold in memory.

Now I resolve problem by using List instead of ConcurrentBag. But I can’t sleep well since I seen this in my memory proflier. Sorry for my eng =)

UPDATE

I change my method “Func”:

void Func(List<int> list)
{
    var bag = new ConcurrentBag<int>();
    Parallel.ForEach(list, k=> bag.Add(++k));

    list.Clear();

    int i;
    while(bag.TryTake(out i))
    {
        list.Add(i);
    }

    bag = null;
    GC.Collect();
    GC.WaitForPendingFinalizers();
}

Then I create project in VS, compile and run my program. This instance graph was been created by “.Net Memory Profiler 4.0” from memory snapshot, that I collect in 10 minutes after program have done all work:

http://xmages.net/storage/10/1/0/f/d/upload/4c67f305.jpg
(sorry, just can’t post images)

  • 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-29T18:36:08+00:00Added an answer on May 29, 2026 at 6:36 pm

    This is because a concurrent bag adds items to the local storage of a thread. The reference from the local storage of a thread to the bag is a strong reference so as long as there is still 1 item in the bag, it ensures that at least 1 thread references the Bag and it wont be collected. Consume all items from the bag or use a different container.

    Please see: Possible memoryleak in ConcurrentBag?

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

Sidebar

Related Questions

Possible Duplicate: General strategy to resolve Java memory leak? I have a standalone program
Possible Duplicate: Anatomy of a “Memory Leak” Hi All what are the best practices
Possible Duplicate: Creating a memory leak with Java There is a Garbage Collector in
Possible Duplicate: how to find memory leak in c++ code/project I would like to
Possible Duplicates: Is it possible to have a memory leak in managed code? (specifically
Possible Duplicate: Creating a memory leak with Java What's the easiest way to cause
Possible Duplicate: Objective C Memory Management My code is showing a memory leak here:
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: C# memory leak? Sorry if this is not the right place to
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions

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.