title: PHP: Having some memory issues inside a loop
body: <p>I have a snippet that resembles the following:</p>
  
  <pre><code>while (true) {
      $myObject = new Class();
      $myOtherObject = $myObject-&gt;getSomeOtherObj();
      ...
      $myArray = [1, 2, 3];
      ...
      sleep(1); //Sleep at the end, to save CPU.
  }
  </code></pre>
  
  <p>This snippet should run as a daemon service, but I'm having a lot of trouble making this work.</p>
  
  <p>The issue: each iteration increases the process memory usage. As if at each new iteration a new <code>$myObject</code> is being instantiated, but the previous one remains allocated in memory, and such.</p>
  
  <p>I have tried:</p>
  
  <ul>
  <li>to <code>unset</code> all variables at the end of the loop (right before the <code>sleep()</code>). </li>
  <li>Setting all variables to <code>null</code>. </li>
  <li>encapsulating them in a separate function (<code>while (true) { doThis(); }</code>)</li>
  <li>manually calling <code>gc_collect_cycles()</code></li>
  </ul>
  
  <p>None of those worked to decrease memory usage.</p>
  
  <p>I have no idea how to force all memory to be released.</p>