15

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.

1
  • 3
    If you have to reallocate huge amount of memory in perl, fork it, allocate memory, process data and exit child.
    – alex
    Jan 4, 2013 at 16:00

2 Answers 2

19

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?

3
  • Wow. Lexical variables never free memory even if they are out of scope? I knew that applied to package variables unless you use undef. I guess this isn't too bad since the largest blocks of data are usually done with references.
    – David W.
    Jan 4, 2013 at 15:11
  • 7
    Perl prefers performance over small memory footprint. You can always undef @a; on lexicals too. It will free the body and string buffers of scalars, and it will free the underlying array of arrays.
    – ikegami
    Jan 4, 2013 at 16:41
  • 6
    If the func() is executed again, there is no considerable amount of memory increase. So I think, it is clear that Perl flags the memory as 'not used' if REFcount = 0, but never returns the memory to OS. This 'not used' flagged memory is used in future for any other allocations.
    – InnovWelt
    Jan 4, 2013 at 17:31
13

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.

1
  • 1
    I did exactly same (before seeing your answer :) ) and i could see that there is no increase in memory. So, I have just returned here to let the people know about this. But Happy to see that @DavidW has already pointed this out.
    – InnovWelt
    Jan 4, 2013 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.