The application uses threads to process incoming connections and one queue for writing to the database. There is a memory leak. Since, apart from the streams and their internal objects, nothing is created there is a suspicion that the streams having worked continue to occupy memory.

How to find out the number of threads in the application?

1 answer 1

  • For .NET >= 3.5 number of active threads can be obtained as follows:

     using (var currentProcess = System.Diagnostics.Process.GetCurrentProcess()) { return process.Threads .OfType<System.Diagnostics.ProcessThread>() .Where(t => t.ThreadState == System.Diagnostics.ThreadState.Running) .Count(); } 
  • As for memory leaks, I recommend reviewing this thread, from which you can get the thesis that you cannot trust the standard tools when monitoring the state of GC .

  • So either "Trust The Garbage Collector", or use specialized profiling utilities.

  • For some reason, intuition tells me that you relied on the values ​​from the Working Set / Private Working Set level in Task Manager, although, of course, I could be wrong.

  • Yes, it looked at the Working set / Private working set (in the Windows Task Manager) - both grow over time, after 2-3 days a little more than 300 MB, from the starting 32-36 MB. Thanks for the link - see what happens in [Process Explorer] [1]. [1]: technet.microsoft.com/en-us/sysinternals/bb896653 - t1nk am
  • It should also be remembered that if there is no memory pressure, then the GC will not seek to free memory. - andreycha
  • Unless in such cases it is not necessary to use HandleCollector (see [Management of the lifetime of objects] [1])? [1]: msdn.microsoft.com/ru-ru/magazine/cc163316.aspx - t1nk