How to identify memory consumption per thread in a process?

拜拜、爱过 提交于 2021-02-19 09:26:37

问题


A multi-thread process written in C exhausts almost all of system memory. To find out the thread which is consuming most of the memory, I made a core file using gcore [pid] to check the memory usage per threads, but I can't find the way to do that.

ps -eLFlm and top command with -H option shows the total memory consumption, but not per thread.

Is there any useful tip to solve the problem?

OS : Centos6


回答1:


A multi-thread process written in C exhausts almost all of system memory. To find out the thread which is consuming most of the memory....

That question does not make sense. By definition, all threads of the same process share the same virtual address space. You could query it programmatically using proc(5) (e.g. reading /proc/self/maps from your program).

It is possible (and quite common) that some heap memory is allocated (e.g. with malloc) in thread A, and would be released (e.g. free-d) later in some other thread B (often the main thread, just before exiting).

The C dynamic memory management heap is, by definition, a whole program property.

A typical example is the last arg argument to pthread_create(3). It generally should be heap-allocated. You could document and adopt the convention that the calling thread (the one using pthread_create) would malloc it, but that the created thread should free it (you could require that each start_routine passed to pthread_create should free that arg).

Is there any useful tip to solve the problem?

Perhaps valgrind might help you finding your memory leaks. You'll better compile all your program (and perhaps some relevant libraries) with DWARF debug information (e.g. compile with gcc -g) then restart your program. But such bugs are difficult to find, so be prepared to spend several weeks on them.


From the conceptual point of view, the "theory" of garbage collection (and also smart pointers, RAII, perhaps reference counting, etc...) could be helpful. So read the GC handbook (it is introducing the good concepts and terminology, and it explains that memory management is a whole program issue). A lot of concepts there are even relevant for programs in non-GC-ed languages like C or C++.

You need to define and follow some good enough whole program conventions regarding memory management (and that is difficult).



来源:https://stackoverflow.com/questions/52601090/how-to-identify-memory-consumption-per-thread-in-a-process

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