0

I'm looking for a way to measure the amount of data stored in a PHP array. I'm not talking about the number of elements in the array (which you can figure out with count($array, COUNT_RECURSIVE)), but the cumulative amount of data from all the keys and their corresponding values. For instance:

array('abc'=>123); // size = 6
array('a'=>1,'b'=>2); // size = 4

As what I'm interested in is order of magnitude rather than the exact amount (I want to compare the processing memory and time usage versus the size of the arrays) I thought about using the following trick:

strlen(print_r($array,true));

However the amount of overhead coming from print_r varies depending on the structure of the array which doesn't give me consistent results:

echo strlen(print_r(array('abc'=>123),true)); // 27
echo strlen(print_r(array('a'=>1,'b'=>2),true)); // 35

Is there a way (ideally in a one-liner and without impacting too much performance as I need to execute this at run-time on production) to measure the amount of data stored in an array in PHP?

3
  • 1
    I don't think your assumptions about the size are entirely correct... but I don't think you'll find anything better than memory_get_usage. Commented Sep 19, 2012 at 9:29
  • there is processing done before the array becomes available which doesn't depend on the array, therefore I cannot use memory_get_usage, memory_get_peak_usage, nor any other function which looks at memory usage from a system standpoint. Commented Sep 19, 2012 at 9:34
  • I guess one could use php.net/manual/en/function.mb-strlen.php Commented Sep 19, 2012 at 9:37

2 Answers 2

1

Does this do the trick:

<?php
    $arr = array('abc'=>123);
    echo strlen(implode('',array_keys($arr)).implode('',$arr));
?>
Sign up to request clarification or add additional context in comments.

1 Comment

works fine for the examples I've given, but not on arrays with a more complex structure such as array('a'=>1,'b'=>array('c'=>3))
0

Short answer: mission impossible

You could try something like:

    strlen(serialize($myArray))     // either this
    strlen(json_encode($myArray))   // or this

But to approximate the true memory footprint of an array, you will have to do a lot more than that. If you're looking for a ballpark estimate, arrays take 3-8x more than their serialized version, depending on what you store in them and how many elements you have. It increases gradually, in bigger and bigger chunks as your array grows. To give you an idea of what's happening, here's an array estimation function I came up with, after many hours of trying, for one-level arrays only:

function estimateArrayFootprint($a) { // copied from one of my failed quests :(
    $size = 0;
    foreach($a as $k=>$v) {
        foreach([$k,$v] as $x) {
            $n = strlen($x);
            do{
                if($n>8192 ) {$n = (1+($n>>12)<<12);break;}
                if($n>1024 ) {$n = (1+($n>> 9)<< 9);break;}
                if($n>512  ) {$n = (1+($n>> 8)<< 8);break;}
                if($n>0    ) {$n = (1+($n>> 5)<< 5);break;}
            }while(0);
            $size += $n + 96;
        }
    }
    return $size;
}

So that's how easy it is, not. And again, it's not a reliable estimation, it probably depends on the PHP memory limit, the architecture, the PHP version and a lot more. The question is how accurately do you need this value.

Also let's not forget that these values came from a memory_get_usage(1) which is not very exact either. PHP allocates memory in big blocks in order to avoid a noticeable overhead as your string/array/whatever else grows, like in a for(...) $x.="yada" situation.

I wish I could say anything more useful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.