简体   繁体   中英

C# : Thread Workload and Memory useage

the Windows Taskmanger is fine to check the CPU and memory useage of an application but in our programm we have different threads and we want to know how much of the total ammount is on each thread.

We would like to check this with an external programm and also from the application itself during runtime. It would be great if a thread could tell about his memory and cpu useage.

Here's is the example: You have threadA and ThreadB. ThreadA creats an object X. ThreadB uses this object. So what do you want to see in thread's information? Who created the object or who is using it? The only thing you can see is how much CPU time is using thread AFAIK And all the same the only program that I know that shows MAX info on process is Process Explorer. http://technet.microsoft.com/en-us/sysinternals/bb896653

You can use performance monitor to see how much memory is allocated to a process, but you cannot see the same for single threads inside it.

However, you could create custom performance counters to display any value you want to monitor from within your code.

SysInternals Process Explorer has this feature, check this Server fault thread .

There is an open source project on CodeProject, the screenshot looks promising: How to get CPU usage of processes and threads , but the demo project seems to be crashing on Win7 (probably missing some privileges).

[Edit] If you want to write it yourself, you can P/Invoke Thread32First and Thread32Next functions to enumerate threads within a single process, and then use QueryThreadCycleTime to query CPU time for each thread.

Objects are shared between threads, threads do not own objects.

Memory for an object is allocated on the heap, which lives in the realm of the application. Any thread can access any of this memory at any time during the lifetime of the application.

There is no way to determine which thread is or may be using any arbitrary blocks of memory.

Threads perform units of work. Unless you know which thread is going to be running which unit of work you will be able to get no reliable metrics out of CPU usage. If you do know which thread will be performing which tasks, then Process Explorer by SysInternals has this metric.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM