I know that Perl uses reference count based garbage collection. When a variable goes out of scope, the reference count is decremented and if REFcount goes to 0, the memory is de-allocated. But when I trace a small example which is shown below, I couldn't able to find the de-allocation happening.
print "start..";
sub func
{
my $length = 8*1024*1024;
my $array = [1..$length];
}
func();
print "done..";
In the example, when the program starts, Perl.exe occupies ~ 3 MB physical memory. After allocation during the func() call, Perl.exe occupies ~ 370 MB memory. But after the func() call , the allocated memory should be garbage collected. why is it not done?
Looking forward for your replies.
According to the question "How can I free an array or hash so my program shrinks?" in perlfaq3:
You usually can't. Memory allocated to lexicals (i.e. my() variables) cannot be reclaimed or reused even if they go out of scope. It is reserved in case the variables come back into scope. Memory allocated to global variables can be reused (within your program) by using undef() and/or delete().
On most operating systems, memory allocated to a program can never be returned to the system. That's why long-running programs sometimes re- exec themselves. Some operating systems (notably, systems that use mmap(2) for allocating large chunks of memory) can reclaim memory that is no longer used, but on such systems, perl must be configured and compiled to use the OS's malloc, not perl's.
In general, memory allocation and de-allocation isn't something you can or should be worrying about much in Perl.
See also How can I make my Perl program take less memory?
Perl may have marked the memory as freed, but it doesn't necessarily mean that it has been freed back to the OS. Your Perl program may reuse that memory. Try running func
again. You shouldn't see an increase in the amount of memory used.
You may want to set the environment variable PERL_DESTRUCT_LEVEL
and see if that makes any difference, but I doubt it.
Garbage collection is not one of Perl's greatest strengths.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With