I want to see the peak memory usage of a command
. I have a parametrized algorithm and I want to know when the program will crash due with an out of memory error on my machine (12GB RAM).
I tried:
/usr/bin/time -f "%M" command
valgrind --tool=massif command
The first one gave me 1414168
(1.4GB; thank you ks1322 for pointing out it is measured in KB!) and valgrind gave me
$ ms_print massif.out
--------------------------------------------------------------------------------
n time(i) total(B) useful-heap(B) extra-heap(B) stacks(B)
--------------------------------------------------------------------------------
75 26,935,731,596 22,420,728 21,956,875 463,853 0
I'm a bit confused which number I should take, but let's assume "total" (22MB).
And the massif-visualizer
shows me
Now I have 3 different numbers for the same command:
valgrind --tool=massif command
+ ms_print
: 22MBvalgrind --tool=massif command
+ massif-visualizer
: 206MB (this is what I see in htop
and I guess this is what I'm interested in)time -f "%M" command
: 1.4GBWhich is the number I should look at? Why are the numbers different at all?
/usr/bin/time -f "%M"
measures the maximum RSS (resident set size), that is the memory used by the process that is in RAM and not swapped out. This memory includes the heap, the stack, the data segment, etc.
This measures the max RSS of the children processes (including grandchildren) taken individually (not the max of the sum of the RSS of the children).
valgrind --tool=massif
, as the documentation says:
measures only heap memory, i.e. memory allocated with malloc, calloc, realloc, memalign, new, new[], and a few other, similar functions. This means it does not directly measure memory allocated with lower-level system calls such as mmap, mremap, and brk
This measures only the memory in the child (not grandchildren). This does not measure the stack nor the text and data segments.
(options likes --pages-as-heap=yes
and --stacks=yes
enable to measure more)
So in your case the differences are:
time
takes into account the grandchildren, while valgrind
does nottime
does not measure the memory swapped out, while valgrind
doestime
measures the stack and data segments, while valgrind
does notYou should now:
valgrind --tool=massif --stacks=yes
to check the stackvalgrind --tool=massif --pages-as-heap=yes
to check the rest of the memory usageIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With