测量静态,堆和堆栈内存? (c ++,Linux-Centos 7) [英] Measure static, heap and stack memory ? (c++, Linux - Centos 7)

查看:152
本文介绍了测量静态,堆和堆栈内存? (c ++,Linux-Centos 7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分别测量堆栈,堆和静态内存,因为我对每个内存都有一些约束.

I would like to measure stack, heap and static memory separately because I have some constraints for each one.

要测量堆内存,我正在使用valgrind-> massif工具. Massif也应该可以测量堆和堆栈内存,但是它显示出奇怪的结果:

To measure the heap memory I'm using valgrind->massif tool. Massif should also be possible to measure heap AND stack memory but it shows strange resultats :

  • 没有--stacks = yes的最后快照提供了total(B)= 0,有用堆(B)= 0,extra-heap(B)= 0(所以一切都很好)

  • Last snapshot without --stacks=yes provides total(B)=0, useful-heap(B)=0, extra-heap(B)=0 (so all is fine)

带有--stacks = yes的最后快照提供了total(B)= 2,256,有用堆(B)= 1,040,Extra-heap(B)= 0,stacks(B)= 1,208(显示内存)即使是相同的命令并测试了相同的二进制文件也会泄漏... dunno为什么...)

Last snapshot with --stacks=yes provides total(B)= 2,256, useful-heap(B)=1,040, extra-heap(B)=0, stacks(B)=1,208 (which shows memory leak even if it is the same command and same binary tested... dunno why ...)

所以最后我需要一个工具来测量c ++二进制文件使用的堆栈和静态内存,欢迎您提供一些帮助:)

So finally I need a tool to measure stack and static memory used by a c++ binary, some help would be welcome :)

感谢您的帮助!

-----------编辑--------------

----------- EDIT --------------

进一步介绍Basile Starynkevitch,以解释我对静态,堆栈和堆内存的含义,我从Dmalloc库文档中获取了它:

Further to the Basile Starynkevitch comment, to explain what I mean with static, stack and heap memory I took it from the Dmalloc library documentation :

  • 静态数据是其存储空间已编译到程序中的信息.

  • Static data is the information whose storage space is compiled into the program.

/* global variables are allocated as static data */
int numbers[10];

main()
{
        …
}

  • 堆栈数据是在运行时分配的数据,用于保存函数内部使用的信息.这些数据由系统在称为堆栈空间的空间中管理.

  • Stack data is data allocated at runtime to hold information used inside of functions. This data is managed by the system in the space called stack space.

    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();
    }
    

  • 堆数据也在运行时分配,并为程序员提供了动态内存功能.

  • Heap data is also allocated at runtime and provides a programmer with dynamic memory capabilities.

    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);
        …
    }
    

  • 推荐答案

    我想分别测量堆栈,堆和静态内存,因为我对每个内存都有一些约束.

    I would like to measure stack, heap and static memory separately because I have some constraints for each one.

    我无法想象为什么每个人都有单独的约束.他们都坐在虚拟内存中!顺便说一句,您可以使用 setrlimit(2)设置限制(也许来自调用shell的过程,例如使用内置的bash ulimit.

    I cannot imagine why you have separate constraints for each one. They all sit in virtual memory! BTW you might use setrlimit(2) to set limits (perhaps from the invoking shell process, e.g. with bash ulimit builtin).

    如果您考虑流程的实际虚拟地址空间,则您的定义很幼稚.

    Your definitions are naive, if you consider the actual virtual address space of your process.

    BTW, proc(5)使您能够查询该空间,例如在程序内部使用/proc/self/maps(如此处)(或/proc/1234/maps来查询pid 1234的过程,也许是从终端).您也可以使用/proc/self/status/proc/self/statm(顺便说一句,请在终端中尝试cat /proc/self/mapscat /proc/$$/maps).在Linux上,您还可以使用 mallinfo(3) malloc_stats(3)以获得有关内存分配统计信息.

    BTW, proc(5) enables you to query that space, e.g. using /proc/self/maps like here from inside your program (or /proc/1234/maps to query the process of pid 1234, perhaps from a terminal). You could also use /proc/self/status and /proc/self/statm (BTW try cat /proc/self/maps and cat /proc/$$/maps in a terminal). On Linux, you might also use mallinfo(3) and malloc_stats(3) to get information about memory allocation statistics.

    静态数据可能位于数据段(或%esp寄存器).而且,多个线程进程具有几个堆栈(和堆栈段),每个线程一个.

    Static data may be in the data segment (or BSS segment) of your program. But what about thread local space? And these data segments also contain data internal to various libraries, notably the C standard library libc.so (do that count?). Of course the stack segment is often bigger (since page aligned) than the actual used stack (from "bottom" to current %esp register). And a multi-threaded process has several stacks (and stack segments), one per thread.

    堆栈数据当然在调用堆栈中,其中包含许多其他内容(返回地址,间隙,溢出的寄存器),而不仅仅是自动变量(其中一些仅位于寄存器中或由编译器进行了优化,而没有占用任何堆栈插槽).他们算吗?另外,来自 crt0 (称为main)的启动代码可能会使用一些一点堆栈空间(计数吗?)...

    Stack data is of course in the call stack, which contains a lot of other things (return addresses, gap, spilled registers) than just automatic variables (some of them sitting only in registers or being optimized by the compiler, without consuming any stack slot). Do they count? Also, startup code from crt0 (which calls your main) is likely to use a little bit of stack space (does it count?)...

    堆分配的数据(可以从各种库分配,甚至可以从动态链接器分配)不仅包含程序从malloc(和朋友)获取的内容,还包含必要的开销.这算吗?那么内存映射文件呢?他们应该怎么计数?

    Heap allocated data (it could be allocated from various libraries, or even from the dynamic linker) contains not only what your program gets from malloc (and friends) but also the necessary overhead. Do that count? And what about memory-mapped files? How should they count?

    我建议查询实际的虚拟地址空间(例如,通过阅读/proc/self/maps或使用

    I would recommend querying the actual virtual address space (e.g. by reading /proc/self/maps or using pmap(1)...) but they you get something different than what you ask.

    这篇关于测量静态,堆和堆栈内存? (c ++,Linux-Centos 7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆