mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   Programming (https://www.mersenneforum.org/forumdisplay.php?f=29)
-   -   Getting memory use of current process (linux)? (https://www.mersenneforum.org/showthread.php?t=4713)

Jushi 2005-09-19 21:27

Getting memory use of current process (linux)?
 
Hello,

I am writing a linux C++ program, and I'm wondering how I can figure out how many memory my program is using. I have looked at getrusage(), but apparently, the memory fields are not supported (and are documented so).
I would just like to print a little diagnostic message along the lines of "This computation ran for %f seconds and used %u MB of RAM".

R.D. Silverman 2005-09-19 22:54

[QUOTE=Jushi]Hello,

I am writing a linux C++ program, and I'm wondering how I can figure out how many memory my program is using. I have looked at getrusage(), but apparently, the memory fields are not supported (and are documented so).
I would just like to print a little diagnostic message along the lines of "This computation ran for %f seconds and used %u MB of RAM".[/QUOTE]

system("ps -elf | grep "myprogram" > tmpfile)

then pull the number out of the tmpfile [I'm not sure which column it will be
in]

ColdFury 2005-09-20 02:39

[QUOTE=Jushi]Hello,

I am writing a linux C++ program, and I'm wondering how I can figure out how many memory my program is using. I have looked at getrusage(), but apparently, the memory fields are not supported (and are documented so).
I would just like to print a little diagnostic message along the lines of "This computation ran for %f seconds and used %u MB of RAM".[/QUOTE]

What kind of memory? Heap? Stack? Virtual memory? Physical memory?

Xyzzy 2005-09-20 02:44

This is, I'm sure, suboptimal, but...

[code]top -b -n 9999 | grep programname | tr -s " " | cut -d " " -f 6-8[/code]

This is how I got memory values for when I did all that GMP-ECM memory profiling...

Jushi 2005-09-20 07:55

[QUOTE=ColdFury]What kind of memory? Heap? Stack? Virtual memory? Physical memory?[/QUOTE]

Good question, I'm not very familiar with all this. I guess either heap space, or total virtual memory would do. This program uses very little stack space, so I don't really care about the stack. And physical will be the same as virtual, since this is a machine without swap.

I'll try the suggestions using system(), but this doens't look like The Right Thing. There has to be a way from the program itself (probably some obscure syscall).

akruppa 2005-09-20 08:37

Does getrusage() do what you want?

Alex

Jushi 2005-09-20 13:14

[QUOTE=akruppa]Does getrusage() do what you want?

Alex[/QUOTE]

Unfortunately not.

According to the man page of getrusage(), the call comes from BSD and is only partially implemented in Linux. In particular, the memory fields are not supported (but the time fields are).

ColdFury 2005-09-21 01:08

[CODE]And physical will be the same as virtual, since this is a machine without swap[/CODE]

Swap space and virtual memory are not the same thing...

mpenguin 2005-09-21 09:39

[QUOTE=Jushi]I have looked at getrusage(), but apparently, the memory fields are not supported (and are documented so).
[/QUOTE]

try reading /proc/<PID>/status and/or /proc/<PID>/statm

or /proc/self/status /proc/self/statm

Jushi 2005-09-22 07:39

[QUOTE=mpenguin]try reading /proc/<PID>/status and/or /proc/<PID>/statm

or /proc/self/status /proc/self/statm[/QUOTE]

Thank you, this seems to work and quite easy to use.

geoff 2005-09-22 13:03

If your kernel has been compiled with the bsd-process-accounting feature (this is the default) then you can use the acct command from the shell (or the acct() function from a C program by including unistd.h) to get the maximum memory used by the process after it exits.


All times are UTC. The time now is 08:01.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.