http://www.perlmonks.org?node_id=59612

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

I would like to report memory usage to a log file at different points in a large perl script. Any suggestions (other than using "top" would be greatly appreciated). jarret

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Is there any way to report mem usage?
by ryan (Pilgrim) on Feb 20, 2001 at 14:01 UTC
    If you are meaning the memory used by your program then there was talk of this here

    If you are talking of the system in general then I might do something crude like this with the /proc file system:

    $meminfo=`cat /proc/meminfo | grep "^Mem: "`; $meminfo =~ s/^Mem\:\W+//; ($total, $used, $free, $shared, $buffered, $cached) = split(/\W+/,$mem +info,6);

    Then you have the 2nd line of /proc/meminfo in 6 variables and can write them out to whatever log you want. There are modules to deal with /proc but I can't find anything to return memory information, only individual process attributes.

    I'm not fantastic at coding, just thought I'd give it a try, I'm sure there is a quicker way - but that is how I would do it with my current knowledge :) I guess this could break if the format of /proc/meminfo was ever changed.

Re: Is there any way to report mem usage?
by salva (Canon) on Jun 02, 2005 at 23:44 UTC
    You can use Proc::ProcessTable to get information about processes including their memory usage (VSize).

    Recently I have also uploaded to CPAN Memchmark, a module that lets measure memory consumption for different algorithms comparing the results, though it is still a very early release.

Re: Is there any way to report mem usage?
by cmeyer (Pilgrim) on Jun 02, 2005 at 23:15 UTC

    The module GTop may be useful to you. The excellent Apache::VMonitor makes use of it for monitoring mod_perl memory usage.

    Be aware that it relies libgtop, a library that is part of the gnome desktop system. This library is not installed on my stock SuSE 9.2 (which uses kde by default). You must have gnome installed, but it is not necessary to run it.

    Sadly, the documentation for GTop is substandard. Look to Apache::VMonitor for better usage examples.