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