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.
>
But that means that as soon as you move to the next iteration of the foreach
loop, both arrays should still go away, right?  Instead, it looks like the
memory is continuously increasing on each iteration.


>
> 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);
>
Ah, my fault.  Yeah, I don't usually name variables the same as functions,
but was coming up with names on the fly as I tried to shrink my script down
to a reproducible test case.

It should have been

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

and unset($registeredBundles).

[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