Garbage Collection of Generic Lists in context of Threads. RRS feed

  • Question

  • From the main application a List<> is created for every 500 items and the list is passed to a Worker Thread.
    With the same reference variable multiple List<>  collections are created.  Seems that garbage collection is not freeing the memory.

    What could be the issue the following code synopsis?

    In Main Thread

         while (DataReader.Read)

    //if it is the very first iteration
    List<workload> MasterList = new List<workload>().
    //Create workload into object objWorkLoad and Add it to MasterList
    //for every 500 items
            //Create a thread and pass the List to the thread .

            //Now create a new List and reference with MasterList.
    MasterList = new List<Class>().




    Worker Thread

        Process the MasterList 
        At the end clear the MasterList  and set null.

    • Edited by cvprocker Saturday, May 31, 2008 5:07 PM change signature
    Friday, May 30, 2008 9:47 PM


  • When you say that garbage collection is not freeing the memory, what exactly do you mean and how do you notice that this is not happening?

    In other words, beyond just watching the working set size in the task manager, what else have you done to verify that you have some kind of leak or lingering reference to your data structures?

    Note that just noticing that memory is slowly increasing doesn't have to mean anything, if you have lots of memory available on your machine (and by lots I really just mean: not a shortage), GC might not kick in if it thinks it won't cause a problem to just allocate more for the time being.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Bruno Yu Wednesday, June 4, 2008 6:32 AM
    Saturday, May 31, 2008 12:50 PM