Removing completed tasks from the <Tasks> list

I am using TPL in .net 4.0 to work with multiple tasks asynchronously. Below is the code snippet:

List<Task> TaskList = new List<Task>();
while (some condition)
{
    var t = Task.Factory.StartNew( () = > doSomething () );
    TaskList.Add(t)
}

//Wait for all tasks to complete
Task.WaitAll(TaskList.toArray());

      

If the while loop runs for a long time, what happens to the size of the "TaskList"? I am concerned that this will take up significant memory if the while loop runs for days. Should I remove completed jobs from this list or are they automatically removed?

Is there any other way to optimize this in terms of memory?

+3


source share


1 answer


TaskList.RemoveAll(x => x.IsCompleted);

      

Do I need to remove completed tasks from this list or automatically remove them?



no, no one will delete your entries List

if you don't.

+6


source







All Articles