Search code examples
linuxheap-memoryvalgrindblock

Used memory in bytes. Linux Heap Block size


I'm using the valgrind to know how many bytes my linux application is using. So, the Valgrind summary show me the number of heap block used. Thus, i'd like to know what is the size of these blocks to know the size of the heap.

here the Heap Summary of the Valgrind:

==2604== HEAP SUMMARY:
==2604==     in use at exit: 4,828,441 bytes in 1,416 blocks
==2604==   total heap usage: 389,448,458 allocs, 389,447,042 frees, 4,664,484,349 bytes allocated
==2604== 

I can't simply do block/bytes because the last block isn't necessarily all allocated. Thanks


Solution

  • Add the option --leak-check=full and valgrind will show a detailed summary of blocks by call-stack. You may need to add also --show-reachable=yes if the blocks are still reachable. From there, you can use some simply math to know the average size of each type of block.

    ==15210== 46,622 bytes in 1,626 blocks are still reachable in loss record 2 of 3
    ==15210==    at 0x4022724: malloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
    ==15210==    by 0x80562A1: (within /bin/ls)
    ==15210==    by 0x80563D1: (within /bin/ls)
    ==15210==    by 0x8053B84: (within /bin/ls)
    ==15210==    by 0x804F686: (within /bin/ls)
    ==15210==    by 0x804FAEB: (within /bin/ls)
    ==15210==    by 0x406F02B: (below main) (in /lib/libc-2.6.1.so)
    ==15210==
    

    That above are blocks of average size:

    $ echo $((46622/1626))
    28