Prev

Perl Notes

Next

Finding memory usage of perl variables

You can also determine the memory usage in bytes of any variable, including multi-dimensional structures (e.g. a hash of hashes, or an array of arrays, or array of hashes etc). To do this, use the Devel::Size module.

Sample CodeOutput
#!/usr/bin/perl
use strict; use warnings; use Devel::Size qw(total_size); my %planet_data = ( 1 => { name => 'Mercury', mass_relative_to_earth => 0.055, }, 2 => { name => 'Venus', mass_relative_to_earth => 0.86, }, 3 => { name => 'Earth', mass_relative_to_earth => 1.0, }, 4 => { name => 'Mars', mass_relative_to_earth => 0.11, }, 5 => { name => 'Jupiter', mass_relative_to_earth => 318, }, ); my $total_size = total_size(\%planet_data);
print "Total Size: $total_size\n";
exit 0;
Total Size: 1158

This answer is somewhat system dependent. To get a better understanding of how Devel::Size measures the size of variables, see perldoc Devel::Size.