Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Is there a way to find the size of a data structure?

by johnnywang (Priest)
on May 16, 2005 at 06:46 UTC ( [id://457376]=perlquestion: print w/replies, xml ) Need Help??

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

Since the internal presentation in C of perl data types contain much more information than the data itself, is there any way to find the actual size, similar to sizeof in C? actually is there even a rough ball-park sizeof? e.g, something like "a scalar string is roughly the string size plus 10 bytes overhead", similar for arrays, references, hashes, objects? (This is not for any practical use, just a curiosity.)
  • Comment on Is there a way to find the size of a data structure?

Replies are listed 'Best First'.
Re: Is there a way to find the size of a data structure?
by gaal (Parson) on May 16, 2005 at 07:08 UTC
    There's Devel::Size. I remember complaints about it not being very accurate, but those were long ago and may have been caused by bugs that have long since been fixed.
Re: Is there a way to find the size of a data structure?
by salva (Canon) on May 16, 2005 at 09:04 UTC
    Lately, I have been rereading "Extended and Embedding Perl" by Tim Jennes and Simon Cozens, and it contains beautiful drawings of the internal structures used by perl to represent data, it's easy to estimate memory consumption looking at them:

    Scalars containing text, use around 4*9+len bytes on 32bit computers. Scalars with numbers sometimes use less memory.

    Arrays use 4*(9+len) bytes plus the memory used by the contained items.

    Hashes are a bit more complex, specially as keys can be shared, but roughly, they eat 4*(13+7*n_items) bytes plus all the key sizes (plus malloc overhead) plus the memory used by the elements.

Re: Is there a way to find the size of a data structure?
by bsdz (Friar) on May 16, 2005 at 11:14 UTC
    You could use Storable, just freeze the structure in memory and print its length, i.e.
    use Storable; my %a = ( 'a' => 'something..', 'b' => 'something else..', ) print length(Storable::freeze(\%a))."\n";
    This will give you a "rough ball-park sizeof" without the memory overhead of using a module like Devel::Size :-)

      Well, that would give the size of the Storable image of the data, not the live-in-RAM size... and I think the OP intended the latter.

      So, for the OP: use Devel::Size, or study the sources...

      -- 
              dakkar - Mobilis in mobile
      

      Most of my code is tested...

      Perl is strongly typed, it just has very few types (Dan)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://457376]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-19 05:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found