hello, this might be more a linux question, but maybe you have experienced this too. I spotted that problem when working with mod_perl and Apache::SizeLimit.

For reproducing I made a simple perl one-liner (well, more like 13 lines) that has a big string (100,000,000) which uses about 200MB RAM.
if I fork this script, on a linux kernel 2.4 all memory is shared, so still 200MB in usage for both. only if I change the string in the child, memory is copied (400MB). I think this is called copy-on-write (correct me if I'm wrong).
now, with a 2.6 kernel on amd64 (debian both systems), almost no memory is shared (like 1MB or so). here's the test script:

perl -we'my $cmd = qq{top -b -n 1 |grep }; print "$$ start\n";print qx{$cmd $$}; my $s="";my $x = "_" x $ARGV[0]; print "string\n";print qx{$cmd $$}; if (my $pid = fork) { print "parent $$ forked $pid\n";print qx{$cmd $$}; $s = "<".$s.">"; } else { print "child $$\n";print qx{$cmd $$}; $s = "(".$s.")";sleep 3; print "child slept 3\n"; } print "$$ end\n";print qx{$cmd $$};' 100000000
perl 5.6.1 and 5.8.4 on kernel 2.6 both show that effect, while 2.4er kernels all share the whole memory (somebody else tested the above script on several machines).

so, question: if this was a bug I would have probably found it with google, but I didn't find anything helpful. what can I do about it, has anyone else experienced this? I guess a pre-forking Apache/mod_perl is pretty common, so there must be others who have the same problem...

update: s/200/400/g and s/100/200/g for correct sizes


In reply to fork on linux 2.6 does not share memory by tinita

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.