Perl Best Practices
19.9. Memory
Don't optimize data structuresmeasure them. Intuitions about the relative space efficiency of different data structures aren't very reliable, either. If you are concerned about the memory footprint of a data structure that you are using, the Devel::Size module makes it easy to see how heavy the burden actually is:
# This look-up table is handy, but seems to be too bloated... my %lookup = load_lookup_table($file); # So let's look at how much memory it's using... use Devel::Size qw( size total_size ); use Perl6::Form; my $hash_mem = size(\%lookup); # Storage overheads only my $total_mem = total_size(\%lookup); # Overheads plus actual data my $data_mem = $total_mem - $hash_mem; # Data only print form( 'hash alone: {>>>,>>>,>>} bytes', $hash_mem, 'data alone: {>>>,>>>,>>} bytes', $data_mem, '============================', 'total: {>>>,>>>,>>} bytes', $total_mem, );
That might print something like: hash alone: 8,704,075 bytes data alone: 8,360,250 bytes ============================== total: 17,064,325 bytes which indicates that storing your 8.36MB of data in a hash has incurred an overhead of an additional 8.70MB for buckets, hash tables, keys, and other internals. The total_size( ) subroutine takes a reference to a variable and returns the total number of bytes of memory used by that variable. This includes both:
The size( ) subroutine also takes a variable reference, but returns only the number of bytes that the variable uses for itself, excluding the memory required to store its data. |