title: Fatal error: Out of memory, but I do have plenty of memory (PHP)
body: <p>Since my question is getting longer and longer, I decide to re-write the whole question to make it better and shorter.</p>
  
  <p>I run my website on dedicated server with 8GB memory. I am fully aware that I need to raise the memory limit on php.ini setting. I have set it from 128M to 256M and to -1. Still the problem is persistence.</p>
  
  <blockquote>
    <p>Fatal error: Out of memory (allocated 786432) (tried to allocate 24576
    bytes) in D:\www\football\views\main.php on line 81</p>
  </blockquote>
  
  <p>The out of memory does not make sense because it said only 786432 bytes is allocated and it needed 24576 bytes more.</p>
  
  <p>786432 bytes is only 768 kilobytes and is fairly small.</p>
  
  <h2>Hints</h2>
  
  <ul>
  <li>The error occurs on a very random line. It does not always error on line number 81.</li>
  <li>At peak time, Apache only takes around 500mb of memory. I still have 6GB to spare.</li>
  <li>There is no infinite loop.</li>
  <li>The script takes 1,042,424 bytes. Getting this number from <code>echo memory_get_peak_usage();</code></li>
  <li>The resultset from MySQL is small (at most 12 of rows, purely text, no blob data)</li>
  <li>(<strong>Important</strong>) If I restart Apache once every two days, the error is gone. It usually happens when Apache is running more than 2 days.</li>
  <li>I have included the profiling the script and you can get it <a href="http://pastebin.com/1WRWCZRs">here</a>.</li>
  <li>This dedicated server is purely used to run only one website. This website is a high traffic website with average of 1,000 visitors every minute. At peak time, there will be 1,700 to 2,000 visitors accessing at the same time.</li>
  </ul>
  
  <h2>Server Spec</h2>
  
  <blockquote>
    <p>OS: Windows 2008 R2 64-Bit<br>
    CPU: Intel Core i5 - 4 cores<br>
    RAM: 8 GB<br>
    Apache 2.2<br>
    PHP 5.3.1<br>
    Storage: 2 x 1 TB hard drives<br>
    Bandwidth: 10 TB per month</p>
  </blockquote>
  
  <h2>Solution</h2>
  
  <p>I have finally tuned up and fixed the problem and I would like to share it here what I have done to improve:</p>
  
  <ol>
  <li><code>favicon.ico</code> was missing which mess up with my route engine. Although my route engine is very small, but by including <code>favicon.ico</code>, it helps reduce memory usage by not running my route engine. Most of part of my website has it and I forgot to put it for this new section.</li>
  <li><p>Limit <code>MaxRequestPerChild</code> helps. In my other dedicated server, I have my <code>MaxRequestPerChild</code> limited. For this server, I set it to 0. I always thought that each script is isolated. Lets say if my script takes 800kb to run. Upon its completion, Apache or PHP should free 800kb memory. It seem like it doesn't work this way. Limited <code>MaxRequestPerChild</code> does help to prevent memory leak by creating new process after limited <code>MaxRequestPerChild</code> and the old process is dying. This is my new setting.</p>
  
  <pre><code>ThreadsPerChild      1500
  MaxRequestsPerChild  10000 
  </code></pre></li>
  <li><p><code>ob_flush();</code> does reduce slightly more memory. It does not help much but every bit of optimization helps.</p></li>
  <li>I have used <code>xdebug</code> which I have never used before as suggested by people who attempt to answer this question. I have to say it is great tool and I have optimized a few stuffs to make it run slightly faster.</li>
  <li>I have disable a few unnecessary Apache module. I am trying to disable it one by one and leave it a few days test to ensure it works perfectly before I disable another one. I do have all unnecessary PHP extension disable now.</li>
  <li>Most of my script in this server used traditional way (no template, no database layer, pure PHP, HTML, and legacy mysql_* function). To be honest, it runs very fast and used extremely small memory. However, maintenance the script is not very easy as the website is getting longer. I have tried to convert some parts of the website into proper framework (my own tiny framework). The reason that I used my own framework because it is tiny (3kb for the whole framework and include only what I need).</li>
  <li><strong>Switching to IIS7.5 solving this problem completely</strong>.</li>
  </ol>