Checking mem usage at specific times in a program

Hi all,

I'm running a simulator and I'm noticing an slow increase in memory for long simulations such that the simulation has to end because of a lack of memory. A colleague of mine ran Valgrind memcheck and reported that nothing of interest was reported other than known mem leaks. My advisor suggested that I try adding getrusage() at suspicious places in the code to try and pinpoint where the leak is occurring in the simulator. I recently tried this only to discover that Unix doesn't fully support getrusage() anymore and it doesn't seem like it will be useful for obtaining memory usage. I know I can use system() to issue a cat and grep to get the mem usage from /proc/pid/status (and I'm not sure if this would be accurate since I guess the system would fork a thread and make that call while the main thread continues?), but I was wondering if there was a better way of determining the memory usage at a particular point in the code. Most of the other solutions I've seen are scripts and would not be able to tell you the difference in memory usage between two different points in the code.

Thanks for any help,

Paul

UNIX does usually fully support getrusage -- you may have to enable XOPEN support. What version of unix do you have?

uname -a

will show you.

If the system is from the last 15 years there will be macro defined that "turns on" XOPEN support. It looks like one of these

#define _XOPEN_SOURCE_EXTENDED
#define _XOPEN_SOURCE nnn <some number goes here, man page tells you>

#define _XOPEN_SOURCE causes restrictions on Darwin - OSX (Apple) rather than extensions.

You have to read your system/compiler documentation (man pages) - specifcally the one for getrusage.

If valgrid reports memory leaks, fix them regardless of what one of your buddies says. Otherwise you are programming by coincidence, not design.

Hey thanks for the reply Jim,

The compute node that I'm running the simulator on is running a recent Ubuntu 64 bit version. Running uname -a yields:

Linux power1 2.6.24-19-generic #1 SMP Wed Aug 20 17:53:40 UTC 2008 x86_64 GNU/Linux

I was not aware of the XOPEN thing. When I did man getrusage, the information didn't say anything about XOPEN. Is this specific to UNIX?
I only did a man getrusage, is there someplace else I should be looking?

Thanks again,

Paul