LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 06-23-2005, 08:29 AM   #1
hae
LQ Newbie
 
Registered: Jun 2005
Posts: 2

Rep: Reputation: 0
How to measure a program's peak memory consumption?


I would like to measure the peak memory consumption of a program with a running time too short for top. Normally, time should do the job. But on my Debian Sarge system it does not:

Code:
> /usr/bin/time <program> <args>
<program output>
2.06user 0.92system 0:03.03elapsed 98%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (174major+1874minor)pagefaults 0swaps
(note the usage of /usr/bin/time to avoid bash's built-in time command)

As you see maxresident is 0 which is obviously wrong as my program consumes awesome lot of memory. This is the case for all other programs I try. The man page for time states that zero values may indicate your Unix flavor does not support their measurement. This is not true for Linux in general I suppose. So why on earth does it not work for me?

All this could be reduced to the wait4 or getrusage system call. I have written a little replacement for time:

(rusage.c)
Code:
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, char** argv, char** envp) {
  int pid, err, ret;
  struct rusage usage;
  if((pid = fork()) == 0) {
    if (execve(argv[0],++argv,envp) == -1) perror("Error during execve: ");
  }
  else {
    if(wait4(pid,&ret,0,&usage) == -1) perror("Error during wait4: ");
    else {
      printf("\n");
      printf(" maximum resident size:  %ld\n",usage.ru_maxrss);
    }
  }
  return ret;
}
wait4 puts the process statistics for the child process into the structure usage. Its field ru_maxrss then should contain the maximum resident size. But here is my output:

Code:
> gcc -o rusage rusage.c
> ./rusage <program> <args>
<program output>

 maximum resident size:  0
I have found out that all process monitoring programs except time use the /proc pseudo-fs to gather their information. Thus one needs to constantly monitor /proc/<pid>/stat to get the peak memory consumption. This is not very elegant in my opinion, as the program does not run for very long, so very small time intervals are needed.
Is there perhaps another, better way to do it?

for the sake of completeness:
Debian Sarge with kernel version 2.4.27-2-686

Manpages: getrusage (2), time (1), proc (5)

Last edited by hae; 06-23-2005 at 08:59 AM.
 
Old 06-23-2005, 09:01 AM   #2
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
I would use /proc/<pid>/stat, it is the more efficient IMHO
 
Old 06-23-2005, 09:39 AM   #3
hae
LQ Newbie
 
Registered: Jun 2005
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by keefaz
I would use /proc/<pid>/stat, it is the more efficient IMHO
This is confusing because:
- wait4/getrusage is called only once, when the process has terminated. As I am not interested in the dynamic behaviour this is totally sufficient.
- /proc/<pid>/stat needs constant monitoring at a rate of, say, 10 ms to get something like the "peak" memory consumption.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
process memory consumption ilnli Linux - General 4 09-08-2005 08:13 PM
Tips on reducing memory consumption Boyer Linux - Software 13 01-17-2005 04:20 PM
Determine process memory consumption ? shayer009 Linux - General 1 10-28-2004 03:03 PM
Slackware memory consumption muhkuhmasta Slackware 5 07-15-2004 06:51 AM
peak memory usage iandc76 Programming 4 01-18-2004 09:23 PM


All times are GMT -5. The time now is 07:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration