In Perl, how can I release memory to the operating system?

前端 未结 4 1313
野性不改
野性不改 2020-12-05 08:30

I am having some problems with memory in Perl. When I fill up a big hash, I can not get the memory to be released back to the OS. When I do the same with a scalar and use

4条回答
  •  情歌与酒
    2020-12-05 09:03

    In general, you cannot expect perl to release memory to the OS.

    See the FAQ: How can I free an array or hash so my program shrinks?.

    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.

    It is always a good idea to read the FAQ list, also installed on your computer, before wasting your time.

    For example, How can I make my Perl program take less memory? is probably relevant to your issue.

提交回复
热议问题