Search code examples
phpmemory

Check how much memory PHP array take


Is there any simple way to check how much memory does some array take?

Like I've got some 10k rows array and need to know how much MB/KB it takes for server to remember it inside some $arr


Solution

  • // how much memory are you using before building your array
    $baseMemory = memory_get_usage(false);
    // create your array
    $a = array('A',1,'B',2);
    // how much memory are you using now the array is built
    $finalMemory = memory_get_usage(false);
    // the difference is a pretty close approximation
    $arrayMemory = $finalMemory - $baseMemory;