Why does the number of threads reported by WinDbg, Task Manager and VS Debugger differ?

蹲街弑〆低调 提交于 2019-12-04 23:07:37

Task manager reports the total number of threads for your process while !threads reports the number of managed threads. If you use the ~ command in WinDbg you will see all thread threads for the process.

The output from !threads in your output shows a lot of dead threads. The threads listed with XXXX for id are threads that have terminated but the corresponding thread objects have not yet been collected. I.e. the reported number is much higher than the actual number of threads. The thread count numbers state that 449 of the 456 threads are dead.

I find the number of threads high and if the application has been idle it is odd that the're still around, but without further info it is hard to be more specific.

My guess is the thread pool had periods of heavy load and then killed the threads when the load died down.

What you're seeing sounds like you have a reference to these threads on a different thread. Since the thread is referenced it can't be Collected by the GC. The thread has completed execution of the method that it was sent to do so it is in neither a sleep state, nor runnable state, so it must be dead. Check your code for a Collection of threads or something similar. Perhaps an event that gets hooked to by the thread, but never unhooked?

Another reason why the thread counts will not match is that Task Manager collects information using noninvasive techniques. A debugger usually, and by default uses an invasive attach.

What this means, is that, any time you debug a running windows application, the debug API inject's a remote thread into the target application and call's KERNEL32!DebugBreak, which is an int3 (0xcc) instruction. At this point the process breaks and stop's executing, however, you now have at least one additional debug thread running.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!