Perl Array Length

I’ll examine below the Perl array length, by which I mean the length in bytes for a Perl array. Secondly, I want to find the memory usage of a Perl array variable.

For computing the Perl array length I’ll use the length function which returns the number of characters of an expression. If you’ll use Unicode characters in your arrays, the number of characters may be different from the number of bytes. Let’s examine the example below:

The @colors is a 2-dimensional array and the snippet code shown above will get us the length in bytes of the @colors array. I used the use bytes pragma, and I put the snippet code in a block {} in order to limit the effect of this pragma to the scope in which it appears, i.e. inside the block. I ran the code on a Windows machine, Perl version 5.8.8 and I got the output: " Perl array length (through length() function) is: 31". This is one way of finding out how big the array is in bytes, but this is not the real memory usage of the array Perl variable.

Next I’ll examine the real length of a Perl array, i.e. the memory usage by that Perl array variable. To allocate an array, Perl uses a contiguous chunk of memory and you must worry about memory issues only in the case you are using large amounts of data. Perl does not limit the amount of storing bytes for a variable, it depends only on the amount of memory that’s available to your process.

Generally speaking, it’s not your task to deallocate the memory used by Perl arrays. If your variables are of my() type, Perl will try to free their storage when they go out of scope.

If you have a global array variable which uses a large amount of bytes for storing it, you can reuse the memory by using undef or delete functions. But, in this last case, the memory used by this variable is fragmented meantime the execution of Perl program, so if you re-allocate it, Perl will use another contiguous chunk of memory for your variable. After some free-allocate process, the memory used by the system will fragment progressively and you could end with some memory issues.

But let’s return to our intention to find out how much memory is used by a Perl array variable. A quick search through the CPAN modules and will get the Devel::Size module which will give us some help to solve this task. You can use this module to determine the memory usage of any Perl variable, including multidimensional arrays.

See the example below to find out how to use the Devel::Size module for getting the Perl array length used by OS to store that variable. Please note that the results will be dependent on your system and your architecture. Of course, in order to test this code on your machine, a prerequisite is to have the Perl Devel::Size module installed.

I used the total_size function to get the amount of memory used by @colors array. When executing, this function follows all the array references, so if we have in view a multidimensional Perl array, the result will be as accurate as possible. I ran this code on a Windows computer, Perl v5.8.8 and I got the output: "Perl array length (through devel::size module) is : 76".

Please compare the results obtained through the two methods for the same array and note that they have different significances.

If you want to dig deeper in the Perl memory allocation, a good idea it is to look for it in Devel::Size module documentation (http://search.cpan.org/~tels/Devel-Size-0.71/lib/Devel/Size.pm).