Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does is_array() leak memory in PHP?

Tags:

php

According to my xdebug output, is_array() leaks the amount of memory that gets passed to it as an argument. If I pass it a large array, it leaks a ton of memory just in order to check if it's an array.

Is there a workaround for this?

   17.4313   21858520   +70004                   -> is_array() [...]/app/app_model.php:526

Here's the code snippet that causes the leak:

        $ret = $this->behaviors[$b[$i]]->afterFind($this, $results, true);
        if (is_array($ret)) {
            $results = $ret;
        }

I'm running this on Linux (Ubuntu 9.04)

PHP: 5.3.2

xdebug: 2.0.5

uname -a gives me this:

Linux linux8 2.6.28-19-server #64-Ubuntu SMP Wed Aug 18 21:57:33 UTC 2010 i686 GNU/Linux

like image 689
MapDot Avatar asked Sep 08 '10 14:09

MapDot


1 Answers

My first reaction:

Select isn't broken.

My second reaction:

You can conclude three things:

  • a widely spread piece of software (is_array) is broken - You are the first one to notice
  • xdebug is broken reports a leak where there is none
  • xdebug and PHP don't work together nicely as it concerns memory management

A widely spread and used function is most often not the problem. Try to narrow down the occurence of the 'xdebug leak report' by running simpler code:

$arr = array_fill( 0, 10000, "content" );
$mallocbytes=true;// set to true to get process 
$usage=memory_get_usage(!$mallocbytes);
for( $i=0; $i!=1000000; $i=$i+1) { 
   is_array($arr); 
   $newusage=memory_get_usage(!$mallocbytes);
   if( $newusage != $usage ) {
      print( "diff after $i'th is_array: ".($newusage-$usage)."\n" );
   }
   $usage=$newusage;
}

Take a look at the actual memory consumption of your PHP runtime. I bet it won't grow.

like image 148
xtofl Avatar answered Sep 23 '22 18:09

xtofl