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 function length() 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:
#!/usr/bin/perl
use warnings;
use strict;
my @colors = (["blue", "red", "green"],
["lightgreen", "lightblue"]);
# using the length() function
{
use bytes;
my $count = 0;
foreach my $item1 (@colors){
foreach my $item2 (@{$item1}){
$count += length($item2);
}
}
print "Perl array length (through length() function) is: ",
$count, "\n";
}
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.
#!/usr/bin/perl
use warnings;
use strict;
my @colors = (["blue", "red", "green"],
["lightgreen", "lightblue"]);
# import the total_size function
use Devel::Size qw(size total_size);
my $develSize = size(@colors);
print "Perl array length (through devel::size module) is : ",
$develSize, "\n";
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).
NEW!!!
Do you want more information about the basic Perl topics?
Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :