简体   繁体   English

C#:线程工作负载和内存使用情况

[英]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. Windows Taskmanger可以检查应用程序的CPU和内存使用情况,但在我们的程序中,我们有不同的线程,我们想知道每个线程上有多少ammount。

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. 如果一个线程可以告诉他的内存和CPU使用情况会很棒。

Here's is the example: You have threadA and ThreadB. 这是示例:您有threadA和ThreadB。 ThreadA creats an object X. ThreadB uses this object. ThreadA创建一个对象X.ThreadB使用该对象。 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. 你唯一能看到的是使用线程AFAIK的CPU时间多少而且我所知道的唯一一个在进程上显示MAX信息的程序是Process Explorer。 http://technet.microsoft.com/en-us/sysinternals/bb896653 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 . SysInternals Process Explorer具有此功能,请检查此服务器故障线程

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). CodeProject上有一个开源项目,截图看起来很有希望: 如何获得进程和线程的CPU使用率 ,但是演示项目似乎在Win7上崩溃(可能缺少一些特权)。

[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. [编辑]如果你想自己编写,可以P / Invoke Thread32FirstThread32Next函数枚举单个进程中的线程,然后使用QueryThreadCycleTime查询每个线程的CPU时间。

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. 除非您知道哪个线程将运行哪个工作单元,否则您将无法获得CPU使用率的可靠指标。 If you do know which thread will be performing which tasks, then Process Explorer by SysInternals has this metric. 如果您确实知道哪个线程将执行哪些任务,则SysInternals的Process Explorer具有此度量标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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