Adam Pietrasiak
Adam Pietrasiak

Reputation: 13184

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

Upvotes: 0

Views: 144

Answers (3)

function memory_Usage($variable) {
    // how much memory does a variable take up?
    $serializedVar = serialize($variable);
    return strlen($serializedVar);
}

Upvotes: -1

Mark Baker
Mark Baker

Reputation: 212402

// 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;

Upvotes: 2

somejkuser
somejkuser

Reputation: 9040

You could look at the following function: http://php.net/manual/en/function.memory-get-usage.php

Upvotes: 1

Related Questions