mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2005-09-19, 21:27   #1
Jushi
 
Jushi's Avatar
 
Sep 2005
UGent

1111002 Posts
Question 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".
Jushi is offline   Reply With Quote
Old 2005-09-19, 22:54   #2
R.D. Silverman
 
R.D. Silverman's Avatar
 
Nov 2003

22·5·373 Posts
Default

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]
R.D. Silverman is offline   Reply With Quote
Old 2005-09-20, 02:39   #3
ColdFury
 
ColdFury's Avatar
 
Aug 2002

26·5 Posts
Default

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?
ColdFury is offline   Reply With Quote
Old 2005-09-20, 02:44   #4
Xyzzy
 
Xyzzy's Avatar
 
Aug 2002

3·112·23 Posts
Default

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...
Xyzzy is offline   Reply With Quote
Old 2005-09-20, 07:55   #5
Jushi
 
Jushi's Avatar
 
Sep 2005
UGent

22×3×5 Posts
Default

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).
Jushi is offline   Reply With Quote
Old 2005-09-20, 08:37   #6
akruppa
 
akruppa's Avatar
 
"Nancy"
Aug 2002
Alexandria

1001101000112 Posts
Default

Does getrusage() do what you want?

Alex
akruppa is offline   Reply With Quote
Old 2005-09-20, 13:14   #7
Jushi
 
Jushi's Avatar
 
Sep 2005
UGent

748 Posts
Unhappy

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).
Jushi is offline   Reply With Quote
Old 2005-09-21, 01:08   #8
ColdFury
 
ColdFury's Avatar
 
Aug 2002

26×5 Posts
Default

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...
ColdFury is offline   Reply With Quote
Old 2005-09-21, 09:39   #9
mpenguin
 
Aug 2005

3×5 Posts
Default

Quote:
Originally Posted by Jushi
I have looked at getrusage(), but apparently, the memory fields are not supported (and are documented so).
try reading /proc/<PID>/status and/or /proc/<PID>/statm

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

Last fiddled with by mpenguin on 2005-09-21 at 09:41 Reason: added /proc/self
mpenguin is offline   Reply With Quote
Old 2005-09-22, 07:39   #10
Jushi
 
Jushi's Avatar
 
Sep 2005
UGent

748 Posts
Default

Quote:
Originally Posted by mpenguin
try reading /proc/<PID>/status and/or /proc/<PID>/statm

or /proc/self/status /proc/self/statm
Thank you, this seems to work and quite easy to use.
Jushi is offline   Reply With Quote
Old 2005-09-22, 13:03   #11
geoff
 
geoff's Avatar
 
Mar 2003
New Zealand

13·89 Posts
Default

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.
geoff is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
'Kernel memory leaking' Intel processor design flaw forces Linux, Windows redesign M344587487 Science & Technology 42 2018-11-17 13:07
One slow worker process aweyhau Hardware 5 2016-02-27 18:57
Restarting a process after it is hung? Xyzzy Linux 29 2014-04-19 14:33
max heap usage of Linux process prasanta Programming 1 2006-11-13 22:49
Use of large memory pages possible with newer linux kernels 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

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

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.