Tobias Schulz has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

How can I make perl release free memory back to the system?
I only found several years old web sites telling me that it's not possible for any program to do that...


I have to run a perl application on a linux server along with other applications consuming much memory. The perl application has to run a long time. During that time it consumes up to 300 MB, but only for a few minutes, then it should shrink to 50 MB, so other applications can use it.


No, it is not possible to use "better" algorithms. And if it would be, that would be no solution.

Maybe there is another malloc implementation?

Thanks, Tobias Schulz

Replies are listed 'Best First'.
Re: Perl - release memory
by Joost (Canon) on Dec 14, 2008 at 12:32 UTC
Re: Perl - release memory
by ysth (Canon) on Dec 14, 2008 at 12:33 UTC
      Are you sure? Other applications exit with an "Out of memory error" after exhausting the swap partition. The perl process doesn't release it.

        The obvious solution to that is "increase your swap." Why worry about memory issues when disk space is so cheap? (I get the same problem at $work, too, and it frustrates me to no end - they don't realise that for about 1 day of my pay, they could get a TB of disk space, and practically never worry about swap space again, whereas NOT doing so will cost me weeks in debugging and reorganisation.)

        Now, assuming your IT management is about as sensical as mine, the solution is (probably - we don't really have enough info to be sure) to fork prior to doing the heavy work, and then exit the subprocess when it's done. The subprocess' memory will be freed, but the parent will continue to live and monitor. It's probably the cheapest approach to the problem.

Re: Perl - release memory
by zentara (Cardinal) on Dec 14, 2008 at 14:52 UTC
    One thing to try is to put your memory intensive code into a thread. The memory should be released back to the system (not just to Perl), when the thread terminates. Try to use the latest version of threads from cpan.

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Perl - release memory
by ysth (Canon) on Dec 14, 2008 at 19:06 UTC
    If increasing your swap space is impossible for political reasons, the next easiest solution is to fork just before the memory intensive part and do it in the child, saving the results to a file for the parent to read if necessary, then exit. (threads, while a possible solution, would be much messier to implement.)
Re: Perl - release memory
by matze77 (Friar) on Dec 14, 2008 at 13:23 UTC

    Maybe your "unix toolbox" (i assume you use a *nix system?) could help, to figure out what components of your program use how much memory (pmap -q processid), lsof e.g.
    You could also tune the "swapiness" of your system to free some memory, Watch how much memory is allocated through shared libraries e.g.
    a google search should help to find the right parameters), eventually your programm could be optimised too, but thats beyond my experience ...

    hth
    MH
Re: Perl - release memory
by Bloodnok (Vicar) on Dec 15, 2008 at 10:12 UTC
    You don't say if the perl application is modifiable by you ... or if you have to manage the memory resources from a sysadmin POV.

    If the former is true, as well as the excellent, predominantly thread orientated, suggestions thus far, it might be worth while checking that, assuming OO perl techniques are used in the application, there aren't a mass of otherwise unwanted objects being unnecessarily 'kept alive', thus consuming memory.

    A user level that continues to overstate my experience :-))