Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get memory usage by the program

Status
Not open for further replies.

sheila11

Programmer
Dec 27, 2000
251
US
Hi all,

I want to know which of my two programms requires lesser memory. Is there a way to know how much memory space is used by your program?

TIA,
Sheila
 
ps v pid

Read the man pages on ps for what each column mean.
 
Sorry, I should have mentioned that I need to get this information thru C++ code. I can't ask the Unix Admn to manually check it for me. My program itself has to fetch it.
 

Why do you need to bother the Admin?

you can run PS on your processes can't you?

I know some places restrict the use of PS agaisnt the whole system but you should be able to see your own program.
 
there are different ways to start a command and read the result in the program.
e.g the 'popen' command is one of them (search this forum for explanation on usage).
execvp is also a command execute a program,theres much info on the web and on this forum (I wrote it :))

1)execute the command ps|grep yourprog1
2)parse the line and get the pid of your program
3 & 4)do the same for your other program
5)execute ps v pid for both your programs and read in the
memory usage.

another way is:

execute:

1)top -1 -b>tempfile //this will dump top in a file
2) read the file and make your conclusions
Hope this helps. Greetz,

The Muppeteer.
themuppeteer@hotmail.com

Don't eat yellow snow...
 
Have you tried using getrusage() yet? I don't know if it is supported on Linux.

zb
 
Hi Sheila,

Here's some piece of code that may be of help to you.
Say you have two programs pgm1 and pgm2:

ps -efl|grep pgm1 |sed 's/ */ /g'|cut -f11 -d' '|pg >pgm1_siz

ps -efl|grep pgm2 |sed 's/ */ /g'|cut -f11 -d' '|pg > pgm2_siz

diff siz_pgm1 siz_pgm2 > diff_pgm12_siz

Note:The eleventh field of "ps -efl" is the memory size of your process

You can do the above steps using system() or popen() from your C++ code.

regards,
VGG

 
Hi sheila,
Sorry on my previous one.
Make this correction:
ps -efl|grep pgm1 |sed 's/ */ /g'|cut -f11 -d' ' >pgm1_siz

ps -efl|grep pgm2 |sed 's/ */ /g'|cut -f11 -d' '> pgm2_siz

diff pgm1_siz pgm2_siz > diff_pgm12_siz

regards,
VGG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top