Guilherme
Guilherme

Reputation: 453

Check how much memory my program uses

I have a C-code program running in my computer and I need to check how much memory my program uses when it is running. Is there any tool that I can use?

PS: Does massif tool (valgrind) work in this case? I already ran it but I don't know how to interpret the graphic created by it.

PPS: I'm using Linux (eOs).

Upvotes: 0

Views: 3987

Answers (3)

K-J
K-J

Reputation: 586

Massif is a great tool that displays how much memory your program uses over time. It has some drawbacks, though:

  • All Valgrind tools are slow; if your program is slow, Massif will slow it down further (by a factor of ten, i think).
  • You have to restart your program to use it, you can't intercept it with Valgrind (and Massif) if your program is already running.
  • You have to compile your program with the '-g' flag if you want the Massif report to show where in the code memory is used.

If you run Massif with your program, you can run

ms_print massif.out.1234 > output

You don't essentially need the 'output'-part, but I find it easier to interpret the results with less (or some other text file reader) than just scrolling through the terminal history. At the top of output, you will see a graph of your memory consumption over time. Below that, you will see some snapshots from arbitrary time intervals from where you can locate the places in code which took most memory.

There are some graphical tools to interpret Massif results, but I have never felt that I require them. Study the report from ms_print and you will learn to interpret its results.

Good luck!

Upvotes: 3

edorado
edorado

Reputation: 375

One way to do is, catching all calls to heap functions such as malloc, realloc, calloc and then summing up the total size. If you don't have this mechanism in your program, you can still do this without changing your program by using LD_PRELOAD mechanism of linux. You just need to compile a small shared library, which has following functions

 void* malloc (size_t size);
 void* calloc (size_t num, size_t size);  
 void* realloc (void* ptr, size_t size);

Then you can implement functions to capture size

 void* malloc (size_t size) {
     totalCount +=  size;
     real_malloc (size_t size)
 }

for details of the implementations you can also have a look in the previous answers Overriding malloc with LD_PRELOAD. Personally I would say just do this mechanism in your program and capture calls internally and count the memory you are allocation.

Upvotes: 0

JstRoRR
JstRoRR

Reputation: 3693

if you are using linux, using some commands you can see the memory consumption by your program, like

top -p Process id 
pmap pid 
ps aux

Simple memory consumption you can check using memcheck tool of Valgrind. To interpret the massif graphs, you should go through the detailed manual. If you are in windows, I guess your task manager would be a great aid. Go through this article for reference.

Upvotes: 1

Related Questions