LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 07: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 07:59 AM.
 
Old 06-23-2005, 08:01 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I would use /proc/<pid>/stat, it is the more efficient IMHO
 
Old 06-23-2005, 08: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



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 07:13 PM
Tips on reducing memory consumption Boyer Linux - Software 13 01-17-2005 03:20 PM
Determine process memory consumption ? shayer009 Linux - General 1 10-28-2004 02:03 PM
Slackware memory consumption muhkuhmasta Slackware 5 07-15-2004 05:51 AM
peak memory usage iandc76 Programming 4 01-18-2004 08:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:03 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration