1

I have a large C# application running under mono. After the application has been running for a while the number of threads begins to increase to the point where the operating system can no longer start new processes.

Using htop I can view a list of all the threads for my application, all one level deep under the application's process. The trouble is they all appear with an identical command to the one that launched the process, i.e. there is no way to tell what each thread is.

If in htop I go to "Display options" -> "Show custom thread names" the thread names show as "Threadpool work".

My question is: how can I determine what is creating these threads? For example is there a mono debugging option to log the line that creates them?

Here is a similar question for Java, but I could not find one for C#.

Community
  • 1
  • 1
Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
  • Can you just write to whatever type of logging you use at the point the thread is being created? Why does your app have so many threads? – khargoosh Apr 11 '17 at 09:20
  • @khargoosh the problem is I don't know where the thread is being created from. I suspect there are so many threads because they are spawning and getting blocked, whereas they should be performing some operation and then exiting. Also they're not necessarily all spawning from the same place. It could be threads from a number of different sources. I just don't really know at this point. – Owen Pauling Apr 11 '17 at 09:27
  • oh I understand. Presumably you know the method that the new thread is entering - could you use one of the StackTrace class methods to log a trace at that point? – khargoosh Apr 11 '17 at 09:33
  • @khargoosh nope I actually have no idea what any of the threads are doing. At the point this is happening I don't expect any new threads to be spawning at all. – Owen Pauling Apr 11 '17 at 09:36
  • In Visual Studio I would probably attach to process then use the parallel stacks window to see what all the threads are doing. Can you do something like that? – khargoosh Apr 11 '17 at 09:46
  • You either should search for `Thread` class usage or `TPL` tasks usage. After that you can try to comment them and see what will stop compile – VMAtm Apr 13 '17 at 20:11
  • Usually there is a factory method that is creating tasks. If your application is using System.Threading, then there should a call to Activator.CreateInstance() – Chris Bartlett May 03 '17 at 16:40

0 Answers0