Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
    
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 '13 at 6:43

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.