Re: PHP Memory Management

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Waynn Lue wrote:
I've been running the script below:

<?php
  $appIds = getLotsOfAppIds();
  foreach ($appIds as $appId) {
    echo "$appId\n";
    //echo memory_get_usage() . "\n";
    try {
      $getBundles = getBundles($appId);
      $numBundles = count($registeredBundles);
      echo $numBundles . "\n";
      continue;
    }
  }
?>

And I get PHP Fatal Error: Allowed Memory Size Exhausted after it runs for a
bit.  Looking at the memory usage, it's because $getBundles (an array) is
huge, and keeps growing.  What I'm confused by is why setting it to
something else in the next iteration of the foreach loop doesn't free the
previously allocated array, since there shouldn't be any more references to
it.  I've worked around it by explicitly calling unset($getBundles), but
just wanted to understand why it's working the way it does.


Aside from on the 1st iteration, each time getBundles() is called, it creates an array within its own scope. Until PHP assigns the returned array to $getBundles, there are two in memory. Maybe that's the problem.

I don't know how you can stand naming structures or variables the same as functions, btw. That would drive me nuts. Where is this $registeredBundles coming from? Perhaps you meant:

$registeredBundles = getBundles($appId);

b

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux