Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Perl not garbage collect memory when a large array is deallocated?

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..";

memory usage at start state

sub func
{
    my $length = 8*1024*1024;
    my $array = [1..$length];

memory usage after allocation

}

func();

print "done..";

memory usage during garbage collection stage

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.

like image 818
InnovWelt Avatar asked Jan 04 '13 14:01

InnovWelt


2 Answers

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?

like image 122
Jonah Bishop Avatar answered Nov 07 '22 12:11

Jonah Bishop


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.

like image 13
David W. Avatar answered Nov 07 '22 10:11

David W.