简体   繁体   English

Linux:如何衡量进程内线程的内存使用情况?

[英]Linux: How to measure memory usage for a thread within process?

I want to measure memory usage for each thread within process. 我想测量进程中每个线程的内存使用情况。 Is it possible? 可能吗? I'm trying to figure out which thread leaks memory. 我想弄清楚哪个线程泄漏了内存。

Edit 1. The pmap for leaking process shows ~600 allocation by [ anon ] 编辑1.泄漏过程的pmap显示[anon]分配~600

...
63b00000    772K rw---    [ anon ]
63bc1000    252K -----    [ anon ]
63c00000    772K rw---    [ anon ]
63cc1000    252K -----    [ anon ]
63d00000    772K rw---    [ anon ]
...

Advice on what to do next? 关于下一步该做什么的建议?

Edit 2. Only virtual memory is leaking eg physical memory usage is stable. 编辑2.仅虚拟内存泄漏,例如物理内存使用稳定。

No this isn't possible, because memory isn't attached to a thread but to the process. 不可能这不可能,因为内存没有附加到线程而是附加到进程。 There is no link between a thread and some part of the memory. 线程与内存的某些部分之间没有链接。

What you seem to need is a profiler, which would point to the allocation points. 你似乎需要的是一个分析器,它将指向分配点。 One of them (didn't use it in the last decade) is Rational Purify . 其中一个(在过去十年中没有使用它)是Rational Purify

You generally can't identify the memory usage of a thread because memory ownership can freely move between threads. 您通常无法识别线程的内存使用情况,因为内存所有权可以在线程之间自由移动。 The kernel mapping tables will show you the usage of the process as a whole, ie the memory allocated for all threads. 内核映射表将向您显示整个过程的用法,即为所有线程分配的内存。

Thread programming is hard . 线程编程很难 Unless you really need to freely share pointers and memory between threads - which is a fairly nasty code smell - it will probably be easier to debug if you rework your program as a flock of processes that communicate over IPC, which will also force you to consider which state needs to be shared. 除非你真的需要在线程之间自由地共享指针和内存 - 这是一个相当令人讨厌的代码味道 - 如果你将你的程序作为一组通过IPC进行通信的进程重新编程,它可能会更容易调试,这也会迫使你考虑哪个州需要分享。 As a bonus, if the leaky process turns out to be a relatively short-lived one, the memory is returned to the system on exit() without you having to locate and patch the leak. 作为奖励,如果泄漏过程变成一个相对短暂的过程,则内存将在exit()返回到系统,而无需定位和修补泄漏。

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

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