import Devel-Size 0.58 from CPAN
[p5sagit/Devel-Size.git] / Size.pm
CommitLineData
e98cedbf 1package Devel::Size;
2
e98cedbf 3use strict;
78dfb4e7 4use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD %EXPORT_TAGS $warn);
e98cedbf 5
6require Exporter;
7require DynaLoader;
8
a6ea0805 9@ISA = qw(Exporter DynaLoader);
e98cedbf 10
11# Items to export into callers namespace by default. Note: do not export
12# names by default without a very good reason. Use EXPORT_OK instead.
13# Do not simply export all your public functions/methods/constants.
14
15# This allows declaration use Devel::Size ':all';
16# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
17# will save memory.
a6ea0805 18%EXPORT_TAGS = ( 'all' => [ qw(
0bff12d8 19 size total_size
e98cedbf 20) ] );
21
a6ea0805 22@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
e98cedbf 23
a6ea0805 24@EXPORT = qw(
e98cedbf 25
26);
78dfb4e7 27$VERSION = '0.58';
e98cedbf 28
29bootstrap Devel::Size $VERSION;
30
78dfb4e7 31$warn = 1;
ebb2c5b9 32
e98cedbf 33# Preloaded methods go here.
34
351;
36__END__
e98cedbf 37
38=head1 NAME
39
0bff12d8 40Devel::Size - Perl extension for finding the memory usage of Perl variables
e98cedbf 41
42=head1 SYNOPSIS
43
0bff12d8 44 use Devel::Size qw(size total_size);
e98cedbf 45
0bff12d8 46 my $size = size("A string");
47
48 my @foo = (1, 2, 3, 4, 5);
49 my $other_size = size(\@foo);
50
51 my $foo = {a => [1, 2, 3],
5c2e1b12 52 b => {a => [1, 3, 4]}
53 };
0bff12d8 54 my $total_size = total_size($foo);
5c2e1b12 55
e98cedbf 56=head1 DESCRIPTION
57
0bff12d8 58This module figures out the real sizes of Perl variables in bytes.
59Call functions with a reference to the variable you want the size
60of. If the variable is a plain scalar it returns the size of
61the scalar. If the variable is a hash or an array, use a reference
62when calling.
63
64=head1 FUNCTIONS
65
66=head2 size($ref)
e98cedbf 67
5c2e1b12 68The C<size> function returns the amount of memory the variable
0bff12d8 69returns. If the variable is a hash or an array, it only reports
70the amount used by the structure, I<not> the contents.
71
72=head2 total_size($ref)
5c2e1b12 73
0bff12d8 74The C<total_size> function will traverse the variable and look
75at the sizes of contents. Any references contained in the variable
76will also be followed, so this function can be used to get the
77total size of a multidimensional data structure. At the moment
78there is no way to get the size of an array or a hash and its
79elements without using this function.
5c2e1b12 80
b98fcdb9 81=head1 EXPORT
e98cedbf 82
0bff12d8 83None but default, but optionally C<size> and C<total_size>.
e98cedbf 84
b98fcdb9 85=head1 UNDERSTANDING MEMORY ALLOCATION
86
87Please note that the following discussion of memory allocation in perl
88is based on the perl 5.8.0 sources. While this is generally
89applicable to all versions of perl, some of the gory details are
90omitted. It also makes some presumptions on how your system memory
91allocator works so, while it will be generally correct, it may not
92exactly reflect your system. (Generally the only issue is the size of
93the constant values we'll talk about, not their existence)
94
95=head2 The C library
96
97It's important firtst to understand how your OS and libraries handle
98memory. When the perl interpreter needs some memory, it asks the C
99runtime library for it, using the C<malloc()> call. C<malloc> has one
100parameter, the size of the memory allocation you want, and returns a
101pointer to that memory. C<malloc> also makes sure that the pointer it
102returns to you is properly aligned. When you're done with the memory
103you hand it back to the library with the C<free()> call. C<free> has
104one parameter, the pointer that C<malloc> returned. There are a couple of interesting ramifications to this.
105
106Because malloc has to return an aligned pointer, it will round up the
107memory allocation to make sure that the memory it returns is aligned
108right. What that alignment is depends on your CPU, OS, and compiler
109settings, but things are generally aligned to either a 4 or 8 byte
110boundary. That means that if you ask for 1 byte, C<malloc> will
111silently round up to either 4 or 8 bytes, though it doesn't tell the
112program making the request, so the extra memory can't be used.
113
114Since C<free> isn't given the size of the memory chunk you're
115freeing, it has to track it another way. Most libraries do this by
116tacking on a length field just before the memory it hands to your
117program. (It's put before the beginning rather than after the end
118because it's less likely to get mangled by program bugs) This size
119field is the size of your platform integer, Generally either 4 or 8
120bytes.
121
122So, if you asked for 1 byte, malloc would build something like this:
123
124 +------------------+
125 | 4 byte length |
126 +------------------+ <----- the pointer malloc returns
127 | your 1 byte |
128 +------------------+
129 | 3 bytes padding |
130 +------------------+
131
132As you can see, you asked for 1 byte but C<malloc> used 8. If your
133integers were 8 bytes rather than 4, C<malloc> would have used 16 bytes
134to satisfy your 1 byte request.
135
136The C memory allocation system also keeps a list of free memory
137chunks, so it can recycle freed memory. For performance reasons, some
138C memory allocation systems put a limit to the number of free
139segments that are on the free list, or only search through a small
140number of memory chunks waiting to be recycled before just
141allocating more memory from the system.
142
143The memory allocation system tries to keep as few chunks on the free
144list as possible. It does this by trying to notice if there are two
145adjacent chunks of memory on the free list and, if there are,
146coalescing them into a single larger chunk. This works pretty well,
147but there are ways to have a lot of memory on the free list yet still
148not have anything that can be allocated. If a program allocates one
149million eight-byte chunks, for example, then frees every other chunk,
150there will be four million bytes of memory on the free list, but none
151of that memory can be handed out to satisfy a request for 10
152bytes. This is what's referred to as a fragmented free list, and can
153be one reason why your program could have a lot of free memory yet
154still not be able to allocate more, or have a huge process size and
155still have almost no memory actually allocated to the program running.
156
157=head2 Perl
158
159Perl's memory allocation scheme is a bit convoluted, and more complex
160than can really be addressed here, but there is one common spot where perl's
161memory allocation is unintuitive, and that's for hash keys.
162
163When you have a hash, each entry has a structure that points to the
164key and the value for that entry. The value is just a pointer to the
165scalar in the entry, and doesn't take up any special amount of
166memory. The key structure holds the hash value for the key, the key
167length, and the key string. (The entry and key structures are
168separate so perl can potentially share keys across multiple hashes)
169
170The entry structure has three pointers in it, and takes up either 12
171or 24 bytes, depending on whether you're on a 32 bit or 64 bit
172system. Since these structures are of fixed size, perl can keep a big
173pool of them internally (generally called an arena) so it doesn't
174have to allocate memory for each one.
175
176The key structure, though, is of variable length because the key
177string is of variable length, so perl has to ask the system for a
178memory allocation for each key. The base size of this structure is
1798 or 16 bytes (once again, depending on whether you're on a 32 bit or
18064 bit system) plus the string length plus two bytes.
181
182Since this memory has to be allocated from the system there's the
183malloc size-field overhead (4 or 8 bytes) plus the alignment bytes (0
184to 7, depending on your system and the key length)
185that get added on to the chunk perl requests. If the key is only 1
186character, and you're on a 32 bit system, the allocation will be 16
187bytes. If the key is 7 characters then the allocation is 24 bytes on
188a 32 bit system. If you're on a 64 bit system the numbers get even
189larger.
190
191This does mean that hashes eat up a I<lot> of memory, both in memory
192Devel::Size can track (the memory actually in the structures and
193strings) and that it can't (the malloc alignment and length overhead).
194
195=head1 DANGERS
196
197Devel::Size, because of the way it works, can consume a
198considerable amount of memory as it runs. It will use five
199pointers, two integers, and two bytes worth of storage, plus
200potential alignment and bucket overhead, per thing it looks at. This
201memory is released at the end, but it may fragment your free pool,
202and will definitely expand your process' memory footprint.
203
e98cedbf 204=head1 BUGS
205
fea63ffa 206Doesn't currently walk all the bits for code refs, formats, and
6a9ad7ec 207IO. Those throw a warning, but a minimum size for them is returned.
e98cedbf 208
b98fcdb9 209Devel::Size only counts the memory that perl actually allocates. It
210doesn't count 'dark' memory--memory that is lost due to fragmented free lists,
211allocation alignments, or C library overhead.
212
e98cedbf 213=head1 AUTHOR
214
215Dan Sugalski dan@sidhe.org
216
217=head1 SEE ALSO
218
219perl(1).
220
221=cut