I always wanted to know memory usage of particular structure in my perl programs. Recently, I noticed that Storable file size corresponds nicely to memory usage of my application, so I used that as a hint of memory usage before loading data.

Then, I decided to show usage of already loaded data. Using Devel::Size total_size seemed like the way to go, but it's much slower than simple Storable piping to /dev/null

Rate total_size storable total_size 4.04/s -- -70% storable 13.5/s 234% --
using this code
use Benchmark qw(:all); use Devel::Size; use Storable; sub _storable_size { open(my $fh, '|-', 'cat > /dev/null'); Storable::store_fd $_[0], $fh; tell($fh); } my $o; $o->{$_} = rand() foreach ( 'a' .. 'z' ); my $hash; $hash->{$_} = $o foreach ( 0 .. 100_000 ); cmpthese( 100, { 'total_size' => sub { Devel::Size::total_size( $hash ) }, 'storable' => sub { _storable_size( $hash ) }, });
To be honest, I didn't expect so much difference. I can probably setup pipes myself to get a bit more performance, but I would really like to know alternative ways to find out perl memory usage without performance impact.

I'm experimenting with this code in gist on github as a log of things that I tried.


2share!2flame...

In reply to Finding memory usage of perl structure efficiently by dpavlin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.