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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:28:25+00:00 2026-06-06T06:28:25+00:00

I am currently developing a small simulation utility, using the Task Parallel Library to

  • 0

I am currently developing a small simulation utility, using the Task Parallel Library to improve the speed at which results are produced. The simulation itself is a long, cpu intensive job which is essentially made up of thousands of smaller jobs running a simulation with different variables.

However, the resources used by each task are not released until everything has completed, leading to memory leaks and out of memory exceptions if enough variables are used. Forcing a GC at the end of each task releases resources, but my understanding is that this needs to interrupt all threads to execute, and as such results in close to single thread performance!

How can I release resources during long operations like this?

By ‘resources’ in this context I’m referring to arrays of doubles… just a lot of them.

public List<AnalysisTask> Questions; //Each variable combination is added as a Q

//Create a task for each simulation
Task<SimulationResults>[] tasks = new Task<SimulationResults>[Questions.Count]; 
foreach(var q in Questions)
{
    AnalysisTask temp = q
    tasks[taskCount] = Task.Factory.StartNew((t) =>
             {
                var result = EvaluateRules(temp);
                if(reults.Value > Leader[0].Value)
                    Leader[0] = result;
                else
                {
                    result.Dispose();
                    //This releases resources but interrupts threads
                    //GC.Collect(2, GCCollectionMode.Forced); 
                    return null;
                }
                return result;

             }
}

//Completion task
Task.Factory.ContinueWhenAll(tasks, (ant) =>
       {
          DoSomethingWithAnswer(Leader[0]);
       }

Perhaps I’ve taken the wrong approach in setting up the tasks? I’ll be grateful for any advice or direction 🙂

  • 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-06T06:28:26+00:00Added an answer on June 6, 2026 at 6:28 am

    Your current implementation has a couple issues. One is that when an exchange is made with Leader[0], the previous leader’s reference is lost and it is never disposed. This could be the source of your memory leak. The second is that the comparison and assignment to Leader[0] are not done atomically. It is possible to have this sequence of events: thread 1 compares to Leader[0] and gets true with a result.Value of 1, thread 2 compares to Leader[0] and gets true with a result.Value of 2, thread 2 writes to Leader[0], thread 1 writes to Leader[0]. The result is that Leader[0] has a value of 1 when the maximal value was 2.

    So if we properly dispose of references you might not need to force garbage collection. The code below fixes those issues by taking out a lock when modifying Leader and storing a reference to the previous Leader[0]. Then either the unused result or previous leader is disposed. Presumably EvaluateRules will take some time so there shouldn’t be much lock contention.

    tasks[taskCount] = Task.Factory.StartNew(() =>
         {
            var result = EvaluateRules(temp);
    
            var toBeDisposed = result;
            lock(Leader) // should be locking on a private object
            {
               if (result.Value > Leader[0].Value)
               {
                 toBeDisposed = Leader[0];
                 Leader[0] = result;
               }
            }
    
            toBeDisposed.Dispose();       
    
         });
    

    Also, do you need to be returning result from each task? You seem to only need Leader[0] for your continuation task. By returning result you are storing a reference that cannot be gc’d until the tasks themselves are gc’d.

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

Sidebar

Related Questions

I'm currently developing a small business database application for which we plan to go
I'm currently developing a small hobby project (open sourced at https://github.com/grav/mailbum ) which quite
I'm currently developing a small WPF application using a file database (SQLCe). Since I'm
I'm currently developing a small jQuery popup modal box. I am using the toggleSlide()
Greetings, Currently developing small web service application where response from web service (using CXF
I'm using Windows XP and currently, I'm developing a small win form application so
I'm currently developing a small OpenGL game for the Android platform and I wonder
Currently developing an application using the newest version of symfony, obtained through PEAR. This
Im currently developing an In-House Enterprise application. I will publish the app using Apple
I'm currently developing a website. Every feature works fine. However, I'm using a lot

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.