title: Memory usage increasing after every loop
body: <p>I have a foreach loop, reading a file containing about 200000 lines, this is my PHP :  </p>
  
  <pre><code>foreach ($this-&gt;file as $row)
        {
          if ($this-&gt;file-&gt;valid())
          {
  
            //init client array
            $this-&gt;initInvoicesArray($row);
            $this-&gt;prepareInvoice();
            $row = null;
            $this-&gt;key = $this-&gt;file-&gt;key();
  
            //add msisdn to msisdn array, and client to clients array
            self::$MsisdnArray[] = $this-&gt;msisdn;
            self::$InvoicesArray[$this-&gt;msisdn] = $this-&gt;client;
  
            if ($i % 3000 == 0) 
            {
              //get valid users from table
              $this-&gt;prepareAndSaveValidClients();
            }
  
            $i++;
          }
        }
  </code></pre>
  
  <p>and this is the prepareAndSaveValidClients() function :</p>
  
  <pre><code>public function prepareAndSaveValidClients(){
      $query = Doctrine_Query::create()
          -&gt;select('p.gender, p.email2, u.username, u.first_name, u.last_name, u.email_address, u.is_active, p.msisdn, p.user_id, p.city_id, p.street, p.zipcode, p.msisdn_status')
          -&gt;from('sfGuardUser u')
          -&gt;innerJoin('u.Profile as p ON u.id = p.user_id')
          -&gt;whereIn('p.msisdn', self::$MsisdnArray)
          -&gt;whereIn('p.status', self::$AllowedStatus);
  
      $results = $query-&gt;fetchArray();
  
      //instanciat an object collection for payment_notifications
      $collection = new Doctrine_Collection("payment_notifications");
  
      if (!empty($results))
      {
        foreach ($results as $key =&gt; $client)
        {
          $invoice = self::$InvoicesArray[$client['Profile']['msisdn']];
  
          $this-&gt;initInvoicesArray($invoice);
          $this-&gt;prepareInvoice();
  
          $this-&gt;prepareUserProfile($client);
          $this-&gt;prepareClient();
  
          $paymentNotifications = new paymentNotifications();
          $paymentNotifications-&gt;fromArray($this-&gt;client);
  
          $collection-&gt;add($paymentNotifications);
  
          $tel = $client['Profile']['msisdn'];
          $client = null;
        }
  
        $collection-&gt;save();
  
        //clear memory
        $results = null;
        $collection = null;
        self::$MsisdnArray = null;
        self::$InvoicesArray = null;
  
  
        $this-&gt;logSection('tel num', 'added :' . $tel . ' Memory usage : ' . memory_get_usage());
        $duration = microtime(true) - $this-&gt;startTime;
        $this-&gt;logSection('payment : ', sprintf('added in %s', $duration));
      }
    }
  </code></pre>
  
  <p>As for the functions :</p>
  
  <pre><code>   $this-&gt;initInvoicesArray($invoice);
     $this-&gt;prepareInvoice();
  
     $this-&gt;prepareUserProfile($client);
     $this-&gt;prepareClient();
  </code></pre>
  
  <p>They are just for preparing <code>$this-&gt;client</code></p>
  
  <p>This is Memory Usage as it's displayed for every loop :</p>
  
  <pre><code>&gt;&gt; tel num   added :0699946185 Memory usage : 89287596
  &gt;&gt; payment :  added in 8.6373870372772
  &gt;&gt; tel num   added :0699983919 Memory usage : 165854544
  &gt;&gt; payment :  added in 18.373502969742
  &gt;&gt; tel num   added :0699949623 Memory usage : 241338788
  &gt;&gt; payment :  added in 29.336947917938
  &gt;&gt; tel num   added :0699854750 Memory usage : 319173092
  &gt;&gt; payment :  added in 40.880628824234
  </code></pre>
  
  <p>As you can see, I tried to free memory of these variables :</p>
  
  <pre><code>$results = null;
  $collection = null;
  self::$MsisdnArray = null;
  self::$InvoicesArray = null;
  </code></pre>
  
  <p>But in vain, Memory Usage keeps increasing after every loop, which results in a An <code>Allowed Memory size exausted</code> fatal error. How can I optimize it? </p>
  
  <p>Thank you.</p>