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

前端 未结 2 1117
迷失自我
迷失自我 2021-02-07 13:03

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

相关标签:
2条回答
  • 2021-02-07 13:26

    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?

    0 讨论(0)
  • 2021-02-07 13:26

    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.

    0 讨论(0)
提交回复
热议问题