echo mb_strlen(serialize((array)$arr), '8bit');
Answer from Masoud Rostami on Stack Overflowecho mb_strlen(serialize((array)$arr), '8bit');
If I understood well your question, you need the size of each "block" subarray inside the main array.
You can do something like this:
$sizes = array();
foreach($returnedArray as $key => $content) {
$sizes[$key] = count($content);
}
The $sizes array will be an associative array which the various "block"s as keys and the size of the data as values.
Edit: after the edit of the question, if the data inside the innermost arrays are strings or integers you can use a function like this:
function getSize($arr) {
$tot = 0;
foreach($arr as $a) {
if (is_array($a)) {
$tot += getSize($a);
}
if (is_string($a)) {
$tot += strlen($a);
}
if (is_int($a)) {
$tot += PHP_INT_SIZE;
}
}
return $tot;
}
assuming to have only ASCII-encoded strings.
memory - Is there a way I get the size of a PHP variable in bytes? - Stack Overflow
caching - PHP: Measure size in kilobytes of a object/array? - Stack Overflow
size (in KB) of variable in php - Stack Overflow
Size of an array in memory in php - Stack Overflow
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
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:
- What's new in PHP V5.2, Part 1: Using the new memory manager
- Memtrack (PECL)
Well, since Memcached doesn't store raw objects (it actually stores the serialiezd version), you can do this:
$serializedFoo = serialize($foo);
if (function_exists('mb_strlen')) {
$size = mb_strlen($serializedFoo, '8bit');
} else {
$size = strlen($serializedFoo);
}
Another easy way pute content of array to file and then check file size.
$str = print_r($array, true);
file_put_contents('data.txt', $str);
$size = filesize('data.txt');