我想分别测量堆栈、堆和静态内存,因为我对每一个都有一些限制。
为了测量堆内存,我使用了 valgrind->massif 工具。Massif 也应该可以测量堆和堆栈内存,但它显示出奇怪的结果:
没有 --stacks=yes 的最后一个快照提供了总(B)=0,有用的堆(B)=0,额外的堆(B)=0(所以一切都很好)
最后一个带有 --stacks=yes 的快照提供了 total(B)= 2,256,有用堆(B)=1,040,extra-heap(B)=0,stacks(B)=1,208(即使它是相同的命令和相同的二进制测试...不知道为什么...)
所以最后我需要一个工具来测量 c++ 二进制文件使用的堆栈和静态内存,欢迎提供一些帮助:)
谢谢你的帮助 !
- - - - - - 编辑 - - - - - - -
除了 Basile Starynkevitch 评论之外,为了解释我对静态、堆栈和堆内存的含义,我从 Dmalloc 库文档中获取了它:
静态数据是其存储空间编译到程序中的信息。
/* global variables are allocated as static data */ int numbers[10]; main() { … }
堆栈数据是在运行时分配的数据,用于保存函数内部使用的信息。该数据由系统在称为堆栈空间的空间中管理。
void foo() { /* if they are, the parameters of the function are stored in the stack */ /* this local variable is stored on the stack */ float total; … } main() { foo(); }
堆数据也在运行时分配,并为程序员提供动态内存能力。
main() { /* the address is stored on the stack */ char * string; … /* * Allocate a string of 10 bytes on the heap. Store the * address in string which is on the stack. */ string = (char *)malloc(10); … /* de-allocate the heap memory now that we're done with it */ (void)free(string); … }