Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

memory usage of modules

by shmem (Chancellor)
on Nov 23, 2009 at 16:03 UTC ( #808839=CUFP: print w/replies, xml ) Need Help??

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


#!/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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://808839]
Approved by Corion
Front-paged by Arunbear
[ambrus]: ad code examples.
[ambrus]: I'm not trying to recommend PHP, but I think it has way too bad a name because of its past.
[ambrus]: This is different from MS Word, which was already a good editor in the pre-unicode days (in word for windows versions 2 and 6, which ran on windows 3 but also on windows 95), only it wasn't trying to solve the task of writing maths papers back then.
[Discipulus]: ah ok, sounds reasonable; with no fear: Perl all life long
[ambrus]: Mind you, LaTeX is currently still useful for writing math paper or snippet content without styling in such a way that the
[ambrus]: formatting conventions of a journal or website can be quickly applied to it, and MS Office and LibreOffice has not quite solved this (although it's better for this than it used to be),
[ambrus]: which is sort of a drawback compared to the ages of typewritten manuscripts representing content only to which the typesetter applies formatting, but that process required much more manual labor.
[ambrus]: If you want to typeset a manuscript, you can still do much less work then in the manual typesetting ages and get good formatting.
[ambrus]: All with only cheap modern computers and software.
[ambrus]: Something you can have at home and your corner print shop, without a whole printing press's worth of equipment.

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2017-09-26 11:12 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (293 votes). Check out past polls.