none
OutOfMemoryException handling ThreadPool RRS feed

  • Question

  • Hi,

     

    I'm new with C# coding and I have this problem. I have a service that needs to run forever in order to monitor one server. In order to do so I have 5 tasks that need to be done by the service (5 at the moment, there are going to be hundreads). I created a threadpool with maximum 5 threads. There is a queue that the threads use in order to get the task, then each thread processes the task and then enqueues it again. The queue contains objects with 5 small attributes.

     

    After the service runs for a while I get an OutOfMemoryException, at the line where I execute this:

    ThreadPool

     

    .QueueUserWorkItem(currentJob.ProcessJob, (object

    )timeToWwait);

    The timeToWwait is an int and I don's use it anymore.

     

    Do you have any suggestions? Please...

    Thanks!

    Oana

     

    Thursday, December 22, 2011 10:57 PM

All replies

  • Hi,

    can you give more details? typically a OutOfMemoryException can happen everywhere where some meomory is allocated. This must not be the location, where the problem exist.

    - Did you check for IDisposable objects that you do not dispose? That is the cause for more memory leaks I have seen.
    - Are you sure, that you release the resources when you are no longer using them? (So do not keep references to objects that you no longer need!)

    And one way is to use a memory profiler to find where you use how much memory. (Maybe see http://stackoverflow.com/questions/49912/best-net-memory-and-performance-profiler)

    Maybe you can give more parts of the code so we can see if we find anything obvious.

    With kind regards,

    Konrad

    Friday, December 23, 2011 10:13 AM
  • When the process crashes with OutOfMemoryException does it seem like the process is taking up a lot of memory? I've had one case where a native error code was interpreted by the .Net framework as an OutOfMemoryException, even though that wasn't the case. Your problem may lie in the fact that you limited the ThreadPool to five threads. Even though your code only needs five threads from the ThreadPool, that doesn't mean that framework code doesn't need additional threads.

    If you remove the limit to the number of Threads the ThreadPool can create, does the number of threads steadily grow? If so, the problem is your work items are never completing.

    Friday, December 23, 2011 3:17 PM
  • Hi,

     

    Thank you for your response and please excuse my delay.

    ThreadPool

     

    .SetMinThreads(0, 0);

     

    ThreadPool

    .SetMaxThreads(5, 5);

     

    while (true

    )

    {

     

    bool processJob = false

    ;

     

    bool checkedFrontEnd = false

    ;

    _qLock.EnterWriteLock();

     

    try

    {

     

    if

    (_jobQueue.Count > 0)

    {

    currentJob = _jobQueue.Dequeue();

    }

    }

     

    finally

    {

    _qLock.ExitWriteLock();

    }

     

    ThreadPool.QueueUserWorkItem(currentJob.ProcessJob, (object)timeToWwait);

    _qLock.EnterWriteLock();

     

    try

    {

    _jobQueue.Enqueue(currentJob);

    }

     

    finally

    {

    _qLock.ExitWriteLock();

    }

    The code is gigantic... I think the problem is as you said with some memory leaks, I just don't know where these happen. I will use the profiler. Thanks a lot for that. I do set the objects I don't need anymore to null. This should help with the memory leaks, right?

     

    Another suspicios thing I discovered is that a big number of System handlers are created... I don't know why or if this may affect the software in such way so that I can get an out of memory exception.

     

    Using Task Manager profiler I see the used memory growing constantly until reaching 3.5GB from 8GB available. So it is not the OS running out of memory, it's the .Net I guess...

     

    I will let you know if I discover anything else.

     

    Thank you so much,

    Oana

    Friday, December 30, 2011 3:09 AM
  • Hi,

     

    Thank you for your response and please excuse my delay.

     

    I get the same error no mather how many threads I use. The jobs are finished, because I have 5 jobs, 5 threads and I can see that the jobs recycle as they should. It looks like a memory leak indeed. I put some code and some facts I noticed in the reply above.

     

    Regards,

    Oana

    Friday, December 30, 2011 3:10 AM