1Devel::Size(3)        User Contributed Perl Documentation       Devel::Size(3)
2
3
4

NAME

6       Devel::Size - Perl extension for finding the memory usage of Perl
7       variables
8

SYNOPSIS

10         use Devel::Size qw(size total_size);
11
12         my $size = size("A string");
13
14         my @foo = (1, 2, 3, 4, 5);
15         my $other_size = size(\@foo);
16
17         my $foo = {a => [1, 2, 3],
18                 b => {a => [1, 3, 4]}
19                };
20         my $total_size = total_size($foo);
21

DESCRIPTION

23       This module figures out the real size of Perl variables in bytes, as
24       accurately as possible.
25
26       Call functions with a reference to the variable you want the size of.
27       If the variable is a plain scalar it returns the size of this scalar.
28       If the variable is a hash or an array, use a reference when calling.
29

FUNCTIONS

31   size($ref)
32       The "size" function returns the amount of memory the variable returns.
33       If the variable is a hash or an array, it only reports the amount used
34       by the structure, not the contents.
35
36   total_size($ref)
37       The "total_size" function will traverse the variable and look at the
38       sizes of contents.  Any references contained in the variable will also
39       be followed, so this function can be used to get the total size of a
40       multidimensional data structure.  At the moment there is no way to get
41       the size of an array or a hash and its elements without using this
42       function.
43

EXPORT

45       None but default, but optionally "size" and "total_size".
46

UNDERSTANDING MEMORY ALLOCATION

48       Please note that the following discussion of memory allocation in perl
49       is based on the perl 5.8.0 sources. While this is generally applicable
50       to all versions of perl, some of the gory details are omitted. It also
51       makes some presumptions on how your system memory allocator works so,
52       while it will be generally correct, it may not exactly reflect your
53       system. (Generally the only issue is the size of the constant values
54       we'll talk about, not their existence)
55
56   The C library
57       It's important firtst to understand how your OS and libraries handle
58       memory. When the perl interpreter needs some memory, it asks the C
59       runtime library for it, using the "malloc()" call. "malloc" has one
60       parameter, the size of the memory allocation you want, and returns a
61       pointer to that memory. "malloc" also makes sure that the pointer it
62       returns to you is properly aligned. When you're done with the memory
63       you hand it back to the library with the "free()" call. "free" has one
64       parameter, the pointer that "malloc" returned. There are a couple of
65       interesting ramifications to this.
66
67       Because malloc has to return an aligned pointer, it will round up the
68       memory allocation to make sure that the memory it returns is aligned
69       right. What that alignment is depends on your CPU, OS, and compiler
70       settings, but things are generally aligned to either a 4 or 8 byte
71       boundary. That means that if you ask for 1 byte, "malloc" will silently
72       round up to either 4 or 8 bytes, though it doesn't tell the program
73       making the request, so the extra memory can't be used.
74
75       Since "free" isn't given the size of the memory chunk you're freeing,
76       it has to track it another way. Most libraries do this by tacking on a
77       length field just before the memory it hands to your program. (It's put
78       before the beginning rather than after the end because it's less likely
79       to get mangled by program bugs) This size field is the size of your
80       platform integer, Generally either 4 or 8 bytes.
81
82       So, if you asked for 1 byte, malloc would build something like this:
83
84            +------------------+
85            | 4 byte length    |
86            +------------------+ <----- the pointer malloc returns
87            | your 1 byte      |
88            +------------------+
89            | 3 bytes padding  |
90            +------------------+
91
92       As you can see, you asked for 1 byte but "malloc" used 8. If your
93       integers were 8 bytes rather than 4, "malloc" would have used 16 bytes
94       to satisfy your 1 byte request.
95
96       The C memory allocation system also keeps a list of free memory chunks,
97       so it can recycle freed memory. For performance reasons, some C memory
98       allocation systems put a limit to the number of free segments that are
99       on the free list, or only search through a small number of memory
100       chunks waiting to be recycled before just allocating more memory from
101       the system.
102
103       The memory allocation system tries to keep as few chunks on the free
104       list as possible. It does this by trying to notice if there are two
105       adjacent chunks of memory on the free list and, if there are,
106       coalescing them into a single larger chunk. This works pretty well, but
107       there are ways to have a lot of memory on the free list yet still not
108       have anything that can be allocated. If a program allocates one million
109       eight-byte chunks, for example, then frees every other chunk, there
110       will be four million bytes of memory on the free list, but none of that
111       memory can be handed out to satisfy a request for 10 bytes. This is
112       what's referred to as a fragmented free list, and can be one reason why
113       your program could have a lot of free memory yet still not be able to
114       allocate more, or have a huge process size and still have almost no
115       memory actually allocated to the program running.
116
117   Perl
118       Perl's memory allocation scheme is a bit convoluted, and more complex
119       than can really be addressed here, but there is one common spot where
120       Perl's memory allocation is unintuitive, and that's for hash keys.
121
122       When you have a hash, each entry has a structure that points to the key
123       and the value for that entry. The value is just a pointer to the scalar
124       in the entry, and doesn't take up any special amount of memory. The key
125       structure holds the hash value for the key, the key length, and the key
126       string. (The entry and key structures are separate so perl can
127       potentially share keys across multiple hashes)
128
129       The entry structure has three pointers in it, and takes up either 12 or
130       24 bytes, depending on whether you're on a 32 bit or 64 bit system.
131       Since these structures are of fixed size, perl can keep a big pool of
132       them internally (generally called an arena) so it doesn't have to
133       allocate memory for each one.
134
135       The key structure, though, is of variable length because the key string
136       is of variable length, so perl has to ask the system for a memory
137       allocation for each key. The base size of this structure is 8 or 16
138       bytes (once again, depending on whether you're on a 32 bit or 64 bit
139       system) plus the string length plus two bytes.
140
141       Since this memory has to be allocated from the system there's the
142       malloc size-field overhead (4 or 8 bytes) plus the alignment bytes (0
143       to 7, depending on your system and the key length) that get added on to
144       the chunk perl requests. If the key is only 1 character, and you're on
145       a 32 bit system, the allocation will be 16 bytes. If the key is 7
146       characters then the allocation is 24 bytes on a 32 bit system. If
147       you're on a 64 bit system the numbers get even larger.
148
149       This does mean that hashes eat up a lot of memory, both in memory
150       Devel::Size can track (the memory actually in the structures and
151       strings) and that it can't (the malloc alignment and length overhead).
152

DANGERS

154       Devel::Size, because of the way it works, can consume a considerable
155       amount of memory as it runs. It will use five pointers, two integers,
156       and two bytes worth of storage, plus potential alignment and bucket
157       overhead, per thing it looks at. This memory is released at the end,
158       but it may fragment your free pool, and will definitely expand your
159       process' memory footprint.
160

Messages: texts originating from this module.

162   Errors
163       "Devel::Size: Unknown variable type"
164           The thing (or something contained within it) that you gave to
165           total_size() was unrecognisable as a Perl entity.
166
167   warnings
168       These messages warn you that for some types, the sizes calculated may
169       not include everything that could be associated with those types. The
170       differences are usually insignificant for most uses of this module.
171
172       These may be disabled by setting
173
174               $Devel::Size::warn = 0
175
176       "Devel::Size: Calculated sizes for CVs are incomplete"
177       "Devel::Size: Calculated sizes for FMs are incomplete"
178

BUGS

180       Doesn't currently walk all the bits for code refs, formats, and IO.
181       Those throw a warning, but a minimum size for them is returned.
182
183       Devel::Size only counts the memory that perl actually allocates. It
184       doesn't count 'dark' memory--memory that is lost due to fragmented free
185       lists, allocation alignments, or C library overhead.
186

AUTHOR

188       Dan Sugalski dan@sidhe.org
189
190       Small portion taken from the B module as shipped with perl 5.6.2.
191
192       Maintained now by Tels <http://bloodgate.com>
193
195       Copyright (C) 2005 Dan Sugalski, Copyright (C) 2007-2008 Tels
196
197       This module is free software; you can redistribute it and/or modify it
198       under the same terms as Perl v5.8.8.
199

SEE ALSO

201       perl(1), Devel::Size::Report.
202
203
204
205perl v5.12.0                      2008-08-23                    Devel::Size(3)
Impressum