Section 19.9.  Memory

Table of Contents

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 memory that the variable uses for its own implementation. For example, the buckets that are needed to implement a hash, or the flag bits that are used inside every scalar.

  • The memory used by the data that the variable stores. For example, the space required for the keys and values in a hash, or for the value in a scalar.

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.

    Table of Contents
    © 2000- NIV