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