title: Perl fork() shared memory
body: <p>Is there any way to load read-only variables into memory before forking and keep them there without using up virt memory x number of children?</p>
  
  <p>It seems to be a common opinion that read-only memory is shared by default and is copied on write. I ran a test and found this to be untrue:</p>
  
  <pre><code>#!/usr/bin/perl
  
  my $data;
  $$data = 'a'x 1_000_000; #keep it in a ref just in case that matters
  
  foreach (0..10){
      last unless my $pid = fork();
  }   
  &lt;STDIN&gt;;
  </code></pre>
  
  <p>While the process is sitting on STDIN, I check top:</p>
  
  <pre><code>  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND   
  
  15982 wdev      20   0  121m 2540  188 S  0.0  0.1   0:00.00 foo.t                             
  15983 wdev      20   0  121m 2540  188 S  0.0  0.1   0:00.00 foo.t                             
  15984 wdev      20   0  121m 2540  188 S  0.0  0.1   0:00.00 foo.t                             
  15985 wdev      20   0  121m 2540  188 S  0.0  0.1   0:00.00 foo.t                             
  15986 wdev      20   0  121m 2540  188 S  0.0  0.1   0:00.00 foo.t                             
  15987 wdev      20   0  121m 2540  188 S  0.0  0.1   0:00.00 foo.t                             
  15988 wdev      20   0  121m 2540  188 S  0.0  0.1   0:00.00 foo.t                             
  15989 wdev      20   0  121m 2540  188 S  0.0  0.1   0:00.00 foo.t                             
  15990 wdev      20   0  121m 2540  188 S  0.0  0.1   0:00.00 foo.t                             
  15991 wdev      20   0  121m 2540  188 S  0.0  0.1   0:00.00 foo.t                             
  15992 wdev      20   0  121m 2540  188 S  0.0  0.1   0:00.00 foo.t  
  </code></pre>
  
  <p>Sure enough, practically all the memory is in individual child processes.</p>
  
  <p>Is there something I am missing, or does forking a perl process really copy over the entire set of data structures for each child?</p>