Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way I get the size of a PHP variable in bytes?

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?

like image 908
EntityDigital Avatar asked Nov 03 '10 10:11

EntityDigital


People also ask

How to check variable size in PHP?

Answer: Use the PHP strlen() function You can simply use the PHP strlen() function to get the length of a string. The strlen() function return the length of the string on success, and 0 if the string is empty.

How many values can a PHP variable hold?

php.net: The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18.


1 Answers

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

like image 139
Haim Evgi Avatar answered Sep 19 '22 05:09

Haim Evgi