mersenneforum.org Getting memory use of current process (linux)?
 Register FAQ Search Today's Posts Mark Forums Read

 2005-09-19, 21:27 #1 Jushi     Sep 2005 UGent 1111002 Posts 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".
2005-09-19, 22:54   #2
R.D. Silverman

Nov 2003

22·5·373 Posts

Quote:
 Originally Posted by 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".
system("ps -elf | grep "myprogram" > tmpfile)

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

2005-09-20, 02:39   #3
ColdFury

Aug 2002

26·5 Posts

Quote:
 Originally Posted by 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".
What kind of memory? Heap? Stack? Virtual memory? Physical memory?

 2005-09-20, 02:44 #4 Xyzzy     Aug 2002 3·112·23 Posts This is, I'm sure, suboptimal, but... Code: top -b -n 9999 | grep programname | tr -s " " | cut -d " " -f 6-8 This is how I got memory values for when I did all that GMP-ECM memory profiling...
2005-09-20, 07:55   #5
Jushi

Sep 2005
UGent

22×3×5 Posts

Quote:
 Originally Posted by ColdFury What kind of memory? Heap? Stack? Virtual memory? Physical memory?
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).

 2005-09-20, 08:37 #6 akruppa     "Nancy" Aug 2002 Alexandria 1001101000112 Posts Does getrusage() do what you want? Alex
2005-09-20, 13:14   #7
Jushi

Sep 2005
UGent

748 Posts

Quote:
 Originally Posted by akruppa Does getrusage() do what you want? Alex
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).

 2005-09-21, 01:08 #8 ColdFury     Aug 2002 26×5 Posts Code: And physical will be the same as virtual, since this is a machine without swap Swap space and virtual memory are not the same thing...
2005-09-21, 09:39   #9
mpenguin

Aug 2005

3×5 Posts

Quote:
 Originally Posted by Jushi I have looked at getrusage(), but apparently, the memory fields are not supported (and are documented so).

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

Last fiddled with by mpenguin on 2005-09-21 at 09:41 Reason: added /proc/self

2005-09-22, 07:39   #10
Jushi

Sep 2005
UGent

748 Posts

Quote:
 Originally Posted by mpenguin try reading /proc//status and/or /proc//statm or /proc/self/status /proc/self/statm
Thank you, this seems to work and quite easy to use.

 2005-09-22, 13:03 #11 geoff     Mar 2003 New Zealand 13·89 Posts 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.

 Similar Threads Thread Thread Starter Forum Replies Last Post M344587487 Science & Technology 42 2018-11-17 13:07 aweyhau Hardware 5 2016-02-27 18:57 Xyzzy Linux 29 2014-04-19 14:33 prasanta Programming 1 2006-11-13 22:49 Dresdenboy Software 3 2003-12-08 14:47

All times are UTC. The time now is 17:53.

Wed Dec 1 17:53:14 UTC 2021 up 131 days, 12:22, 2 users, load averages: 1.85, 1.47, 1.37