http://qs1969.pair.com?node_id=808839

Sometimes I want to know the amount of memory a Module takes.

package MemImpact; use vars qw($before $after $now); BEGIN { $before = `ps -o vsz= -p $$`; } $now = $before; printf "%-40s % 10d kB\n", "initial memory:" , $before; sub import { my $pack = shift; for (@_) { eval "use $_;"; die "Can't load $_:\n$@\n" if $@; $after = `ps -o vsz= -p $$`; printf "%-40s % 10d kB\n", "after loading $_:" , $after; printf "%-40s % 10d kB\n", "impact of $_:" , $after - $now; $now = $after; } } END { printf "%-40s % 10d kB\n", "total impact:" , $after - $before; } 1;

Usage example:

perl -MMemImpact=Moose,MooseX::AttributeHelpers -e 1

or

#!/usr/bin/perl use MemImpact qw(Moose); use MemImpact qw(MooseX::AttributeHelpers);
initial memory: 7576 kB after loading Moose: 13408 kB impact of Moose: 5832 kB after loading MooseX::AttributeHelpers: 14848 kB impact of MooseX::AttributeHelpers: 1440 kB total impact: 7272 kB

Replies are listed 'Best First'.
Re: memory usage of modules
by BioLion (Curate) on Nov 23, 2009 at 18:45 UTC

    This looks pretty useful - I guess it could also be used (with a little tinkering) to return current memory usage - or even have a simple memory usage object with methods for giving the difference from another object of the same class. This way people could use the function however they liked, say doing one bit of analysis and finding out how their usage has changed etc... and shoot warnings / death if usage is too high etc...

    There is Devel::Size but that isn't really the same thing, and neither is Benchmark::Harness::MemoryUsage (which also doesn't seem to be actively developed any more). I don't know if there is a general usage memory monitor, it would be really useful.

    Just a something something...
      I don't know if there is a general usage memory monitor

      GTop, a binding to libgtop, is kind of the library version of the ps and top commands... (GTop::ProcMem and GTop::Mem for checking memory usage).

      Advantage: less overhead than calling an external executable and parsing its output (though only relevant when being called many many times).

      Disadvantage: not a core module.

        Ah well ;-) GTop... this Module also has the advantage that you can query any kind of size, and you get it in bytes. Voilà:

        package MemImpact::GTop; use strict; use GTop; my @meths = qw(size vsize rss share); my $before = GTop->new->proc_mem($$); my $after = my $now = $before; printf "%-60s % 10d Bytes\n", "$_ initial memory:" , $before->$_ for @meths; print "-" x 77, $/; sub import { my $pack = shift; my $meth = shift; for (@_) { eval "use $_;"; die "Can't load $_:\n$@\n" if $@; $after = GTop->new->proc_mem($$); printf "%-60s % 10d Bytes\n", "$meth after loading $_:" , $aft +er->$meth; printf "%-60s % 10d Bytes\n", "$meth impact of $_:" , $after-> +$meth - $now->$meth; $now = $after; } } END { print "-" x 77, $/; printf "%-60s % 10d Bytes\n", "$_ total impact:" , $after->$_ - $b +efore->$_ for @meths; } 1;
        qwurx [shmem] ~ > perl -e 'use MemImpact::GTop qw(rss Moose); use MemI +mpact::GTop qw(rss MooseX::AttributeHelpers)' size initial memory: 934707 +2 Bytes vsize initial memory: 934707 +2 Bytes rss initial memory: 346112 +0 Bytes share initial memory: 250265 +6 Bytes ---------------------------------------------------------------------- +------- rss after loading Moose: 897433 +6 Bytes rss impact of Moose: 551321 +6 Bytes rss after loading MooseX::AttributeHelpers: 1043660 +8 Bytes rss impact of MooseX::AttributeHelpers: 146227 +2 Bytes ---------------------------------------------------------------------- +------- size total impact: 711065 +6 Bytes vsize total impact: 711065 +6 Bytes rss total impact: 697548 +8 Bytes share total impact: 35225 +6 Bytes