2. actually return memory to the OS. This is, in general, not possible. In *nix, once a process is allocated pages, there is no mechanism for giving them back. The only exception to this is memory added via mmap. (...) mmap, however, is typically only used for very specific things, like shared-lib access

But this seems to be just what is happening (i.e. mmap/munmap):

PID SZ VSZ 5645 604 11064 PID SZ VSZ 5645 4516 14976 PID SZ VSZ 5645 2560 13020

(This is the output I get when running Joost's snippet — even on an old Linux box with a rather rusty 2.6.16 kernel.)

The last memory printout is definately about 2M smaller (the PV/string part of that scalar variable) than the previous one...

And strace shows — when I replace the system("/bin/ps",...) with a simple print "---\n" (to leave a marker in the output):

$ strace ./746953.pl (...) brk(0x66a000) = 0x66a000 read(4, "#!/usr/bin/perl\n\nsub ps {\n #s"..., 8192) = 1003 fcntl(4, F_SETFD, FD_CLOEXEC) = 0 write(1, "---\n", 4--- ) = 4 mmap(NULL, 2002944, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, - +1, 0) = 0x2ba8bbdd5000 mmap(NULL, 2002944, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, - +1, 0) = 0x2ba8bbfbe000 write(1, "---\n", 4--- ) = 4 munmap(0x2ba8bbfbe000, 2002944) = 0 write(1, "---\n", 4--- ) = 4 lseek(4, 196, SEEK_SET) = 196 lseek(4, 0, SEEK_CUR) = 196 close(4) = 0 exit_group(0)

In reply to Re^2: demonstrate that perl can give back memory to the OS by almut
in thread demonstrate that perl can give back memory to the OS by perl5ever

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.