Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I am trying to view the memory used when a program is executed, is there any way to do this?

The program takes a few seconds to run, but I would like to view the memory it consumed when it did, because it doesnt run for long it makes it near impossible to view on a top or ps command

Any tips appreciated

Replies are listed 'Best First'.
Re: Memory Usage
by Fletch (Bishop) on Apr 12, 2002 at 01:39 UTC

    Use GTop. Have it snapshot memory usage in an END block. Or insert a sleep 15 before your exit so you have time for using ps.

Re: Memory Usage
by sfink (Deacon) on Apr 12, 2002 at 02:06 UTC
    This answer isn't going to have much to do with perl.

    On some Unix systems, you can use /usr/bin/time --verbose command args.... (Be careful -- many shells have a builtin time command, usually with far fewer options.) It has the advantages of telling you the maximum memory used, too. This will not work on Linux (most of the numbers will be zero since they're not recorded.)

    On Linux, try reading the file /proc/self/status at various points in processing. Within the END{} block is good, but you might also want to do it earlier in case perl cleans up memory for you before executing the END code.

    Probably most portably, run ps -l $$ from within your program (or use whatever flags work on your system and give you good memory-related information.) Example:perl -le '$ps=`ps -v $$`'

    If you have a perl built with -DDEBUGGING, you might also try the -m flag to perl itself. You'll need to postprocess that, though.