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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.