title: Deliberately eating certain amount of memory using perl script
body: <p>I need to simulate a memory-hungry process. For example, On a machine with 4.0 GiB, I need a process that would eat 3.2 GiB (give or take few MiB).</p>
  
  <p>I assumed it should be as easy as:</p>
  
  <pre><code>my $mbytes = 3276;
  my $huge_string = 'X' x ($mbytes * 1024 * 1024);
  </code></pre>
  
  <p>But I end up with process eating twice as much memory as I need it to.</p>
  
  <ul>
  <li><p>this is same on two Windows 7 amd64 machines: one with 64-bit, the other
  with 32-bit build of Strawberry Perl</p></li>
  <li><p>I'm using Sysinternals Process Explorer and watching "Private Bytes"</p></li>
  </ul>
  
  <p>Of course, I could just <code>$mbytes /= 2</code> (for now, I'll probably will do that), but:</p>
  
  <ul>
  <li><p>Is there a better way?</p></li>
  <li><p>Can anyone explain why the amount is twice as length of the string?</p></li>
  </ul>