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