Linux 如何获取程序的堆大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9748792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get Heap size of a program
提问by Anu
How to find heap memory size of a c++ program under linux platform ?I need heap memory space before the usage of new or malloc and also after that.can anyone help?
如何在 linux 平台下找到 c++ 程序的堆内存大小?在使用 new 或 malloc 之前以及之后我需要堆内存空间。有人可以帮忙吗?
#include <malloc.h>
#include <iostream>
int main()
{
//here need heap memory space
unsigned char* I2C_Read_Data= new unsigned char[250];
//get heap memory space After the usage of new
return 0;
}
回答by Gangadhar
回答by TheBuzzSaw
You can also add heap tracking to your own programs by overloading the new
and delete
operators. In a game engineI am working on, I have all memory allocation going through special functions, which attach each allocation to a particular heap tracker object. This way, at any given moment, I can pull up a report and see how much memory is being taken up by entities, actors, Lua scripts, etc.
您还可以通过重载new
anddelete
运算符将堆跟踪添加到您自己的程序中。在我正在开发的游戏引擎中,我通过特殊函数分配所有内存,这些函数将每个分配附加到特定的堆跟踪器对象。这样,在任何给定时刻,我都可以调出报告,查看实体、演员、Lua 脚本等占用了多少内存。
It's not as thorough as using an external profiler (particularly when outside libraries handle their own memory management), but it is very nice for seeing exactly what memory you were responsible for.
它不像使用外部分析器那么彻底(特别是当外部库处理它们自己的内存管理时),但是很高兴准确地查看您负责的内存。
回答by Matthieu M.
Apart from external inspection, you can also instrument your implementation of malloc to let you inspect those statistics. jemalloc
and tcmalloc
are implementations that, on top of performing better for multithreaded code that typical libc implementations, add some utility functions of that sort.
除了外部检查,您还可以检测您的 malloc 实现,让您检查这些统计信息。jemalloc
并且tcmalloc
是一些实现,除了对典型 libc 实现的多线程代码性能更好之外,还添加了一些此类实用程序函数。
To dig deeper, you should learn a bit more how heap allocation works. Ultimately, the OS is the one assigning memory to processes as they ask for it, however requests to the OS (syscalls) are slower than regular calls, so in general an implementation of malloc
will request large chunks to the OS (4KB or 8KB blocks are common) and the subdivise them to serve them to its callers.
要深入挖掘,您应该更多地了解堆分配的工作原理。最终,操作系统是根据进程的要求为进程分配内存的人,但是对操作系统的请求(系统调用)比常规调用慢,因此通常的实现malloc
将向操作系统请求大块(4KB 或 8KB 块是common) 并将它们细分为它们的调用者。
You need to identify whether you are interested in the total memory consumed by the process (which includes the code itself), the memory the process requested from the OS within a particular procedure call, the memory actually in use by the malloc
implementation (which adds its own book-keeping overhead, however small) or the memory yourequested.
您需要确定您是否对进程消耗的总内存(包括代码本身)、进程在特定过程调用中从操作系统请求的内存、malloc
实现实际使用的内存(添加其自己的簿记开销,无论多小)或您请求的内存。
Also, fragmentation can be a pain for the latter two, and may somewhat blurs the differences between really used and assigned to.
此外,碎片化对于后两者来说可能是一种痛苦,并且可能会在某种程度上模糊真正使用和分配给之间的差异。
回答by Vitali
On Linux you can read /proc/[pid]/statm
to get memory usage information.
在 Linux 上,您可以读取/proc/[pid]/statm
以获取内存使用信息。
Provides information about memory usage, measured in pages. The columns are:
size total program size (same as VmSize in /proc/[pid]/status) resident resident set size (same as VmRSS in /proc/[pid]/status) share shared pages (from shared mappings) text text (code) lib library (unused in Linux 2.6) data data + stack dt dirty pages (unused in Linux 2.6)
提供有关内存使用情况的信息,以页为单位。列是:
size total program size (same as VmSize in /proc/[pid]/status) resident resident set size (same as VmRSS in /proc/[pid]/status) share shared pages (from shared mappings) text text (code) lib library (unused in Linux 2.6) data data + stack dt dirty pages (unused in Linux 2.6)
See the man pagefor more details.
有关更多详细信息,请参阅手册页。
Answer by Adam Zalcman to this questiondescribes some interesting details of the heap allocation
Adam Zalcman 对这个问题的回答描述了堆分配的一些有趣细节
回答by Erik Aronesty
You can try "mallinfo" and "malloc_info". They might work. mallinfo has issues when you allocate more than 2GB. malloc_info is o/s specific and notably very weird. I agree - very often it's nice to do this stuff without 3rd party tools.
您可以尝试“mallinfo”和“malloc_info”。他们可能会工作。当您分配超过 2GB 时, mallinfo 会出现问题。malloc_info 是特定于 o/s 的,而且非常奇怪。我同意 - 通常情况下,在没有 3rd 方工具的情况下做这些事情很好。