You are not logged in.

#1 2005-09-17 19:26:57

fluke
Member
From: Shaoguan Univ., PRC
Registered: 2005-08-12
Posts: 241
Website

What's the best way to reckon up the memory usage of a prog

What's the best way to reckon up the memory usage of a program?

I want to know how much memory is used by a program, and how long it toke to run.

Offline

#2 2005-09-18 04:18:12

awalk
Member
From: Perth, Western Australia
Registered: 2005-02-14
Posts: 40

Re: What's the best way to reckon up the memory usage of a prog

GNU "time" should do the trick; but it would seem Arch doesn't package it.

You can use the 'getrusage' C function (or from within a scripting language like Python, if you prefer). I believe GNU time just wraps getrusage.

EDIT: actually, that looks like it only gets the resident set size...

Offline

#3 2005-09-18 05:29:09

Snowman
Developer/Forum Fellow
From: Montreal, Canada
Registered: 2004-08-20
Posts: 5,212

Re: What's the best way to reckon up the memory usage of a prog

awalk wrote:

GNU "time" should do the trick; but it would seem Arch doesn't package it.

I have time on my system.  I think it's part of the shell as it seems that there is no time binary.

Offline

#4 2005-09-18 09:38:00

awalk
Member
From: Perth, Western Australia
Registered: 2005-02-14
Posts: 40

Re: What's the best way to reckon up the memory usage of a prog

Yep, that's a builtin shell command. There's an actual standalone program called 'time' too. http://directory.fsf.org/GNU/time.html

Sorry, should've put that in the first post... smile

Offline

#5 2005-09-18 17:56:57

Snowman
Developer/Forum Fellow
From: Montreal, Canada
Registered: 2004-08-20
Posts: 5,212

Re: What's the best way to reckon up the memory usage of a prog

No problem. I'll try to do a PKGBUILD for it.

EDIT: There was already a PKGBUILD in AUR.  roll  http://aur.archlinux.org/packages.php?d … =1&ID=1010
I've posted a few comments to improve it, though.

Offline

#6 2005-09-20 04:39:56

fluke
Member
From: Shaoguan Univ., PRC
Registered: 2005-08-12
Posts: 241
Website

Re: What's the best way to reckon up the memory usage of a prog

And how to get the memory-spend info?

Offline

#7 2005-09-20 05:50:48

Snowman
Developer/Forum Fellow
From: Montreal, Canada
Registered: 2004-08-20
Posts: 5,212

Re: What's the best way to reckon up the memory usage of a prog

I've check the output more closely. I get 0 for the memory usage.

man time (GNU VERSION section) wrote:

Not all resources are measured by all versions of Unix, so some of the  values might be reported as zero.

I don't know if it's a problem with my system or with Linux. sad

Offline

#8 2005-09-20 06:24:33

fluke
Member
From: Shaoguan Univ., PRC
Registered: 2005-08-12
Posts: 241
Website

Re: What's the best way to reckon up the memory usage of a prog

Yes, I check the man page too.
But I cant really understand the whole meaning since I'm not a English  speaker:(
I do it like this:

time -p "mem-usage %Kn" foo

Please correct me.
Thank you.

Offline

#9 2005-09-20 06:58:09

Snowman
Developer/Forum Fellow
From: Montreal, Canada
Registered: 2004-08-20
Posts: 5,212

Re: What's the best way to reckon up the memory usage of a prog

First, you need to use the full path or use the gnutime link as I mentioned in the AUR, otherwise you'll use the built-in shell 'time' command which only gives the execution time. In your example, you forgot the -f in front of the string:

 /usr/bin/time -p -f"mem-usage %Kn" foo

There's also the %M which is the maximum real memory actually used by the program.  I am not too familiar myself with these technical terms. And the fact that I only get 0 don't help.

Offline

#10 2005-09-20 11:36:55

fluke
Member
From: Shaoguan Univ., PRC
Registered: 2005-08-12
Posts: 241
Website

Re: What's the best way to reckon up the memory usage of a prog

Yes, I get 0 too. Strange.

Offline

#11 2005-09-20 11:41:19

fluke
Member
From: Shaoguan Univ., PRC
Registered: 2005-08-12
Posts: 241
Website

Re: What's the best way to reckon up the memory usage of a prog

I run the gnutime in verbose mode, and these are all I get:

        Command being timed: "mozilla"
        User time (seconds): 2.16
        System time (seconds): 0.23
        Percent of CPU this job got: 22%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:10.78
        Average shared text size (kbytes): 0
        Average unshared data size (kbytes): 0
        Average stack size (kbytes): 0
        Average total size (kbytes): 0
        Maximum resident set size (kbytes): 0
        Average resident set size (kbytes): 0
        Major (requiring I/O) page faults: 0
        Minor (reclaiming a frame) page faults: 16873
        Voluntary context switches: 833
        Involuntary context switches: 1722
        Swaps: 0
        File system inputs: 0
        File system outputs: 0
        Socket messages sent: 0
        Socket messages received: 0
        Signals delivered: 0
        Page size (bytes): 4096
        Exit status: 0

Anyone can help me?

Offline

#12 2005-09-20 12:18:39

dtw
Forum Fellow
From: UK
Registered: 2004-08-03
Posts: 4,439
Website

Re: What's the best way to reckon up the memory usage of a prog

Maybe you should just try

man top

and look for memory. Go the easy route...

Offline

#13 2005-09-20 12:52:03

awalk
Member
From: Perth, Western Australia
Registered: 2005-02-14
Posts: 40

Re: What's the best way to reckon up the memory usage of a prog

Or, if you need to do it from a program, cat /proc/pid/status ... surely there's a better way, though.

Offline

#14 2005-09-20 17:56:21

fluke
Member
From: Shaoguan Univ., PRC
Registered: 2005-08-12
Posts: 241
Website

Re: What's the best way to reckon up the memory usage of a prog

For some reasons, I must run a program and get run infos immediately.
Please, help me.

Offline

#15 2005-09-20 17:59:57

fluke
Member
From: Shaoguan Univ., PRC
Registered: 2005-08-12
Posts: 241
Website

Re: What's the best way to reckon up the memory usage of a prog

That means, I don't know the pid, and usually, the program runs in a very short time.

Offline

#16 2005-09-20 18:07:20

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: What's the best way to reckon up the memory usage of a prog

I prefer htop to find memory hungry programs.

fluke wrote:

That means, I don't know the pid, and usually, the program runs in a very short time.

pgrep procressname

will give you the process number. Or you can do

kill -TERM `pgrep proccessname`

to find and terminate in one shot.

Offline

#17 2005-09-21 03:33:44

awalk
Member
From: Perth, Western Australia
Registered: 2005-02-14
Posts: 40

Re: What's the best way to reckon up the memory usage of a prog

D'oh! Just found this at the very bottom of the man page.

The above struct was taken from 4.3BSD Reno.  Not all fields are  meaningful  under Linux.  In linux 2.4 only the fields ru_utime, ru_stime, ru_minflt, and ru_majflt are maintained.  Since Linux 2.6, ru_nvcsw and ru_nivcsw are also maintained.

Offline

#18 2005-09-21 18:04:02

fluke
Member
From: Shaoguan Univ., PRC
Registered: 2005-08-12
Posts: 241
Website

Re: What's the best way to reckon up the memory usage of a prog

The Program will be run many times in a short time too. That I can get its pid correctly.
Im waiting for a better solution.

Offline

#19 2005-09-22 09:09:37

fluke
Member
From: Shaoguan Univ., PRC
Registered: 2005-08-12
Posts: 241
Website

Re: What's the best way to reckon up the memory usage of a prog

Have you ever used this function - setrlimit?
http://www.opengroup.org/onlinepubs/007 … limit.html

Offline

#20 2005-09-22 17:55:04

fluke
Member
From: Shaoguan Univ., PRC
Registered: 2005-08-12
Posts: 241
Website

Re: What's the best way to reckon up the memory usage of a prog

Who can tell me the python way?
I'm using python now.

Offline

Board footer

Powered by FluxBB