track C++ memory allocations

前端 未结 8 1869
执笔经年
执笔经年 2020-11-28 04:18

I am looking for a way to track memory allocations in a C++ program. I am not interested in memory leaks, which seem to be what most tools are trying to find, but r

8条回答
  •  眼角桃花
    2020-11-28 04:24

    Use Valgrind and its tool Massif. Its example output (a part of it):

    99.48% (20,000B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
    ->49.74% (10,000B) 0x804841A: main (example.c:20)
    | 
    ->39.79% (8,000B) 0x80483C2: g (example.c:5)
    | ->19.90% (4,000B) 0x80483E2: f (example.c:11)
    | | ->19.90% (4,000B) 0x8048431: main (example.c:23)
    | |   
    | ->19.90% (4,000B) 0x8048436: main (example.c:25)
    |   
    ->09.95% (2,000B) 0x80483DA: f (example.c:10)
      ->09.95% (2,000B) 0x8048431: main (example.c:23)
    

    So, you will get detailed information:

    • WHO allocated the memory (functions: g(), f(), and main() in above example); you also get complete backtrace leading to allocating function,
    • to WHICH data structure the memory did go (no data structures in above example),
    • WHEN it happened,
    • what PERCENTAGE of all allocated memory it is (g: 39.7%, f: 9.95%, main: 49.7%).

    Here is Massif manual

    You can track heap allocation as well as stack allocation (turned off by default).

    PS. I just read that you're on Windows. I will leave the answer though, because it gives a picture of what you can get from a possible tool.

提交回复
热议问题