4

Get process memory consumption this question is about getting the memory usage of running program. I need to know how much RAM a program has used after it has finished.

Say I am compiling a C++ program with this command g++ main.cpp. I need to know how much memory this used after it has finished. Or, if memory can only be measured for a specific instant, I need to know what is the maximum RAM usage when compiling a program.

For any program I passed to /usr/bin/time -f "%M", I get max resident value 0. I am sure that my program uses gigabytes of memory

0

3 Answers 3

6

Run the program under /usr/bin/time and look at the max resident value. You need to specify the full path to avoid using the simpler builtin time command.

1
  • For any program I passed to /usr/bin/time -f "%M", I get max resident value 0, I am sure that my program uses gigabytes of memory
    – Ashot
    May 23, 2013 at 6:43
3

I always test my code with valgrind.

run your code as

valgrind --leak-check=yes --track-origins=yes ./a.out

It provides a good output about memory consumption (allocation/deallocation), but be prepared for a very slow run.

Using the -g flag when compiling will help valgrind to provide information about which lines may leak memory. (an extra treat)

Feel free to check for additional valgrind parameters for additional output. For me these two are enough.

0

Short of modifying the kernel or running the program under investigation within some sort of wrapper that records its memory usage, your best bet is to take the advice given in the answers to the linked question.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .