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

Hello!! I really need to know how perl internally stores things: how many bytes to data types take up, references to arrays, hashes, scalars, scalars themselves, etc. Basically, i need to get the total size of a data structure in memory. I can't find this info anywhere, please help. thank you eduard grinvald

Replies are listed 'Best First'.
Re: Perl internals: sizes
by chromatic (Archbishop) on Mar 04, 2001 at 03:40 UTC
    The mod_perl Performance Guide suggests the Gtop module. If you're able to create your data structure in one swoop, you can get a good feel for its size with code similar to:
    use GTop; my $gtop = GTop->new; my $before = $gtop->proc_mem($$)->rss; # initialize your data structure here my $after = $gtop->proc_mem($$)->rss; print "diff: ",$after-$before, " bytes\n";

    As the Guide says, you'll need libgtop installed. It's only a workable solution if you're running a free sort of operating system, however.

Re: Perl internals: sizes
by AgentM (Curate) on Mar 04, 2001 at 03:26 UTC
    For this, you'd need to learn a little perlxs. It is probably very easy to use Inline to hook yourself up with sizeof() and some SV function to pull this info. Perl alone provides no such mechanism unless you consider Data::Dumper to give you a rough estimate of data in the structure (it will not tell you how much memory Perl allocates along with it). Good luck!
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
Re: Perl internals: sizes
by jdhedden (Deacon) on Jan 16, 2006 at 00:20 UTC
    Devel::Size provides this capability.

    Remember: There's always one more bug.