Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Virtual Memory Usage

by pileofrogs (Priest)
on Feb 06, 2006 at 21:04 UTC ( [id://528330] : perlquestion . print w/replies, xml ) Need Help??

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

I'm trying to figure out how much virtual memory my perl script is using. I've looked at node 498401 and node 502486, and it looks like Devel::Size is pretty close to what I want, but not quite.

Essentially, I'm using BSD::Resource to limit my script, and I'd like to know how much of these resources I'm actually using. The list of resources you can monitor with BSD::Resource::getrusage is smiler but actually very different from the list of resources you can limit. Specifically, you can limit virtual memory, but you can't look at your virtual memory usage. You can look at resident set size, but that doesn't seem to help me. (it always seems to say 0, even when I bust the vmem limit).

Devel::Size seems to work by walking through the code and totaling up the memory used. The system obviously has some simple value of my vmem size (otherwise, how does it know how to limit it?), so Devel::Size sounds inefficient for my purposes.

Anyone know a good way to get current vmem used?


Updated: This needs to work on Linux and OpenBSD.

Replies are listed 'Best First'.
Re: Virtual Memory Usage
by tirwhan (Abbot) on Feb 06, 2006 at 23:07 UTC

    You didn't specify, but if you're doing this on Linux you can use /proc/self/stat (see man proc for the details).

    open my $stat,"<","/proc/self/stat" or die "Can't open /proc/self/stat +"; my ($vsz,$rss)=(split ' ',<$stat>)[22,23]; print "$vsz - $rss\n"

    Or you could use the ps command or Unix::Process

    All dogma is stupid.
Re: Virtual Memory Usage
by diego_de_lima (Beadle) on Feb 06, 2006 at 21:49 UTC
    Try GTop.

    Diego de Lima
Re: Virtual Memory Usage
by Qiang (Friar) on Feb 09, 2006 at 05:50 UTC
    there was this article on memory usage on slashdot few days ago. good read.

    to sum up the article, in linux, application uses shared libraries and the shared libraries are being shared among other applications. but it is still only one copy of shared library. ps aux gives you the memory usage on the process itself and the shared library too.

    so, try run ps aux to grab your perl proc id.then run pmap -d Proc_ID. the mapped: result at the bottom line is the total memory usage including shared libraries. writeable/private is the one without counting the shared libraries.

    my sshd pmap output:

    mapped: 3652K writeable/private: 804K shared: 0K