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

I was wondering what the "state of research" is on enabling Perl to release some of it's memory pool back to the system, before it exits. I've googled on this topic, and the only thing I've seen is "we are working on it". That gives me hope that it is indeed possible. Does anyone know if Perl6 is trying to make this possible?

I'm not really a human, but I play one on earth. flash japh
  • Comment on Perl releasing memory back to the system

Replies are listed 'Best First'.
Re: Perl releasing memory back to the system
by Joost (Canon) on Apr 07, 2005 at 17:25 UTC
    AFAIK, perl 5.8.6 (and probably a couple of earlier versions) can release memory back to the system on Linux.

    #!/usr/local/bin/perl -w use strict; $|=1; { my $string; for (1 .. 100000) { $string .= ('x' x 1000); } print "press enter to release"; <>; undef $string; # note that memory does not get released # if the variable only goes out of scope. # you *need* to undef it! } print "ok, press enter to exit"; <>;

    This takes up roughly a 100 Mb on my machine, and almost all of it gets released after the first enter. (according to top, at least)

    perl -V Summary of my perl5 (revision 5 version 8 subversion 6) configuration: Platform: osname=linux, osvers=2.6.8.20050102, archname=i686-linux
      Thanks! Great example, works for me. It is the first time I've seen this demonstrated with Perl. :-)

      I'm not really a human, but I play one on earth. flash japh
Re: Perl releasing memory back to the system
by Limbic~Region (Chancellor) on Apr 07, 2005 at 13:57 UTC
    zentara,
    As far as I know, this is dependent on the OS and possibly on how Perl was compiled (with or without system malloc). If the OS doesn't support releasing the memory then Perl6 isn't going to make it possible. Perl6 may make systems that do support the release more likely though. Typically, the OS is going to want to get large contiguous chunks. Perl6 and Parrot are likely to be more like C in the way they use memory then existing Perl is.

    I am not a subject matter expert and this has been discussed here several times before. Super Search should shed even more light on the matter.

    Cheers - L~R

      It looks complicated, but does indeed look hopeful. From the new Parrot 0.12 release:
      Parrot 0.1.2 contains a lot of new stuff: .... Parts of a generational garbage collector ....
      Whatever that is, it sounds good, :-)

      I'm not really a human, but I play one on earth. flash japh

        The generational GC means that a garbage collection run doesn't need to inspect everything every time it runs. That is, it is possible to ask it to only inspect some subset of the total allocations during a given run. Useful when you know that you have just released a bunch of stuff local to some level of stack framing, and you can free that with incurring the cost of having everything prior to the current level also inspected.

        It has nothing to do with releasing memory back to the OS.

        In general, Perl takes the view that if you asked for a lump of memory once, then freed it, then you are quite likely to ask for it again, so why bother giving it back to the OS. If you don't make further use of it, and other tasks require more memory than is physically available, then the unused portions of your task space in Perl will likely get swapped to disk. If you never make further use of them, then very little overhead is incurred by it's presence.

        What is your concern with releasing memory back to the OS?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco.
        Rule 1 has a caveat! -- Who broke the cabal?
Re: Perl releasing memory back to the system
by dmitri (Priest) on Apr 07, 2005 at 21:03 UTC