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 inhtopand I guess this is what I'm interested in)time -f "%M" command: 1.4GB
Which 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: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=yesand--stacks=yesenable to measure more)So in your case the differences are:
timetakes into account the grandchildren, whilevalgrinddoes nottimedoes not measure the memory swapped out, whilevalgrinddoestimemeasures the stack and data segments, whilevalgrinddoes notYou should now:
valgrind --tool=massif --stacks=yesto check the stackvalgrind --tool=massif --pages-as-heap=yesto check the rest of the memory usage