title: Why PHP is allocating too much memory than the loaded image?
body: <p>I executed the following PHP code:</p>
  <pre><code>$a = memory_get_usage();
  $b = memory_get_usage(true);
  echo $a.' bytes ('.($a/1024/1024).' MB), '.$b.' bytes ('.($b/1024/1024).' MB)&lt;br&gt;';
  
  $img = imagecreatefromjpeg('image.jpg');
  
  $a = memory_get_usage();
  $b = memory_get_usage(true);
  echo $a.' bytes ('.($a/1024/1024).' MB), '.$b.' bytes ('.($b/1024/1024).' MB)';
  </code></pre>
  <p>The size of the image is <code>8.8 MB</code> with resolution <code>8185 x 5787</code>.</p>
  <p>The output was as following:</p>
  <pre><code>396168 bytes (0.37781524658203 MB), 2097152 bytes (2 MB)
  190081952 bytes (181.27627563477 MB), 127926272 bytes (122 MB)
  </code></pre>
  <p>As can be seen, before loading the image, PHP/script's memory usage was only <code>0.3778.. MB</code> (or maximum <code>2 MB</code>) but after loading the image of <code>8.8 MB</code>, the memory usage increased to <code>181 MB</code> (<code>181 / 8.8 = 20.57</code> times than the image)!</p>
  <p>Why PHP is using too much memory than the loaded image? It is understandable to use 2 to 5 times, but 20.57 times doesn't seems too much?</p>
  <hr />
  <p>And another thing, this is what documentation of PHP <code>memory_get_usage([ bool $real_usage = FALSE ] ) : int</code> is saying about it's parameter:</p>
  <blockquote>
  <p>If not set or FALSE only the used memory is reported. Set this to TRUE to get total memory allocated from system, including unused pages.</p>
  </blockquote>
  <p>I find it confusing. In the first line of output, the <strong>used memory</strong> is <code>0.3778 MB</code> while <strong>total memory allocated from system</strong> is <code>2 MB</code>, which seems fine. But in the second line of output, <strong>used memory</strong> is <code>181 MB</code> while <strong>total memory allocated from system</strong> is <code>122 MB</code>. How can total memory be less than the used memory?</p>