Q: How can I find out how much memory my scalar/array/hash/multidimensional data structure uses?
A: Use Devel::Size
Devel::Size is a module designed specifically to figure out how much real memory a variable is using. This counts all the memory, including all of perl's internal bookkeeping and cached memory, not just what you might be able to see from the perl level. (Including things like the memory used for magic, the variable-sized structures that represent scalars of different types, and preallocated extra bits for arrays and hashes)
Here's a quick example of its use, from the docs:
It does follow references when using the total_size function, and does make sure that no structure is counted more than once, so it's safe to use with self-referential data structures.use Devel::Size qw(size total_size); my $size = size("A string"); my @foo = (1, 2, 3, 4, 5); my $other_size = size(\@foo); my $foo = {a => [1, 2, 3], b => {a => [1, 3, 4]} }; my $total_size = total_size($foo);
At the moment its one failing is that it doesn't handle code refs quite completely (it doesn't trace through the scratchpads) and so will complain if you try it on a code ref, or something that has a code ref. It will do its best, though, and count up the memory that it does know how to find. It also won't trace memory that isn't perl's, for example memory that an external library has allocated for itself.
Thanks are due to kudra who both wrote the docs and prodded this module into existence in the first place.
Disclaimer: I wrote the module, so this isn't exactly an unsolicited testimonial :)
In reply to Finding the size of a variable by Elian
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |