Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


  Search this Thread
Old 06-23-2005, 08:29 AM   #1
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:

> /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:

#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(" 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:

> 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
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 5,397

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

Original Poster
Rep: Reputation: 0
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.


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 > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:57 PM.

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