[英]How to get the peak dynamicly allocated memory usage of a C/C++ program in Linux system
I would like to know the peak dynamicly allocated memory usage (have already canceled the released memory usage) after running a C/C++
program.我想知道运行
C/C++
程序后动态分配memory使用量的峰值(已经取消释放memory使用量)。
1 (Initial question): 1(初始问题):
Given a C/C++ program a.out
.给定一个 C/C++ 程序
a.out
。 Can any tool report the peak dynamically allocated memory usage in a way like:任何工具都可以通过以下方式报告动态分配的峰值 memory 使用情况:
>$ peak-memory ./a.out
Peak dynamically allocated memory size: 12345678 Bytes
2 (Alternative question): 2(备选问题):
Can I insert a snippit code in the source program such that every time it is executed, it will report the current heap memory usage where I want.我可以在源程序中插入一个片段代码,这样每次执行时,它都会在我想要的地方报告当前堆 memory 的使用情况。 This looks like:
这看起来像:
int main(){
int *a = (int*) malloc(12);
// some code...
print_heap_usage();
// other code...
}
My research:我的研究:
I know I can use a wrapper function such as my_malloc
and my_free
that track the allocated and released memory. But it is not a practical option for other's source code when too many lines of code need to be modified.我知道我可以使用一个包装器 function,例如
my_malloc
和my_free
来跟踪分配和释放的 memory。但是当需要修改太多代码行时,这对于其他人的源代码来说不是一个实用的选择。 And prohibitively, this wrapper function can't handle the situation when new
and delete
are used.令人望而却步的是,这个包装器 function 无法处理使用
new
和delete
时的情况。
There is one related question Get peak amount of memory used by a C program .有一个相关问题Get peak amount of memory used by a C program 。 But it's only about Windows system and doesn't specifically care about heap memory usage.
但它只是关于 Windows 系统,并不特别关心堆 memory 的使用情况。 Some answers indicated the using of
getrusage
.一些答案表明使用
getrusage
。 However, I googled and found it clearly can't tell the heap usage.但是,我搜索了一下,发现它显然无法分辨堆的使用情况。
Either任何一个
Statistics for Memory Allocation with malloc Memory 与 malloc 分配的统计
or或者
Note : check the CONFORMING TO sections.注意:检查符合部分。
Another gimmick (in terminal):另一个噱头(在终端):
cat /proc/meminfo
free
(from procps package, uses /proc/meminfo, see https://gitlab.com/procps-ng/procps/-/blob/master/proc/sysinfo.c line 698 in function meminfo
) free
(来自 procps package,使用 /proc/meminfo,请参阅https://gitlab.com/procps-ng/procps/-/blob/master/proc/sysinfo.c function meminfo
中的第 698 行)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.