13

I currently have a PHP CLI script using Zend Framework extensively which seems to be using a ever larger amount of memory as it runs. It loops through a large set of models retrieved from a database in batches of 1000. Calls to memory_get_usage() show that the memory usage of the script is always increasing.

This is despite making sure that I'm unsetting the model after every iteration and actually using array_shift() to reduce the size of the array of models on every iteration.

My question is, in PHP is there a way of discovering the size-in-memory of a variable so I can track what's growing?

Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
EntityDigital
  • 133
  • 1
  • 5
  • There's a [duplicate](http://stackoverflow.com/questions/1075419/how-to-find-memory-used-by-an-object-in-php-sizeof) but I'm not happy with the accepted answer there (although the solution may be the only way) so not voting to close and +1 – Pekka Nov 03 '10 at 10:10
  • This is a question i've struggled with for a while. It's possible to use xDebug to profile an app, but it says nothing about memory usage. Short of calling memory_get_usage at certain points, I've never found a solution. – Neil Aitken Nov 03 '10 at 10:18
  • I did have a search for duplicates but all I got was a page full of out of memory errors, my bad though. The reason I accepted that particular answer was that it basically confirmed what we had literally just discovered, there's no real way of discovering the size of a variable and, at least part of the reason it was chewing memory was because of bad garbage collection. I see your point though. – EntityDigital Nov 03 '10 at 11:33

4 Answers4

4

i don't have a solution to check the size of every variable, but if you use doctrine its probably the reason

you need to use

   $Elem->free(true);

another thing is to upgrade to 5.3 (if you dont do it yet), the garbage collector of 5.3 is better

Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
  • We're not using doctrine but running it on 5.3 massively improved things, especially running gc_collect_cycles() after each batch of 1000. Thanks. – EntityDigital Nov 03 '10 at 10:54
3

No. You are likely looking for memory that is not freed, e.g. you unlinked a variable or deleted a reference and the garbage collector did not yet release the associated block in memory.

You could try Zend Server 5 (you need the commercial version though) to mem-profile your application. It has code tracing. I dont know if this would allow you to spot memory leaks though.

Also see:

Gordon
  • 312,688
  • 75
  • 539
  • 559
0

I don't know how accurate it is, but I got a number by using apc_add('variable_name', $var);. I then go to my apc.php under user cache entries and look at the size column.

Of course for this to work you need to have APC installed and running. :-)

Icode4food
  • 8,504
  • 16
  • 61
  • 93
-2

Here is a code snippet I found at weberdev

<?php
function array_size($a){
    $size = 0;
    while(list($k, $v) = each($a)){
        $size += is_array($v) ? array_size($v) : strlen($v);
    }
    return $size;
}
?>

It gets the size of the given array in bytes. Is this what you meant?

Cpt. eMco
  • 565
  • 4
  • 13
  • 3
    This returns the total length of the strings inside an array, it says nothing about the size of the array in bytes. If I was using a multibyte character set, this function would report incorrectly – Neil Aitken Nov 03 '10 at 10:14
  • You're right, but most of the time, you won't be using a multibyte charset and since it is used for debugging purposes, maybe it can help. – Cpt. eMco Nov 03 '10 at 10:18
  • 1
    Looking at the original question, the OP is retrieving a large set of model items, so it's likely to be an array of objects rather than strings. This function could be useful in cerain cases, but I'm not sure it's applicable here. – Neil Aitken Nov 03 '10 at 10:22
  • 1
    What about array keys? does that count their sizes as well? – AbiusX Oct 19 '11 at 10:28
  • 1
    @NeilAitken Sorry but you are wrong. `strlen` gives the number of bytes in a string, *not* the number of characters. `mb_strlen` gives the number of characters, counting multi-byte characters as 1. – andrewtweber Jun 27 '12 at 21:47