4

I am trying to get REAL size (memory usage) of a variable in PHP. I know there is no straightforward method to achieve this but there is a simple "hack" using memory_get_usage().

<?php

function varSize()
{
    $s = memory_get_usage();
    $x = true;
    echo memory_get_usage() - $s;
}

varSize();

echo '<br>';

$s = memory_get_usage();
$x = true;
echo memory_get_usage() - $s;

echo '<br>';

$s = memory_get_usage();
$x = unserialize(serialize(true));
echo memory_get_usage() - $s;

?>

This code returns 64, 160, 0 respectively. The hell why? The two first variants is an absolute copy-paste of each other! Why is this happens and how to get a real variable size?

3
  • 1
    usage here means allocated; so in the 2nd example it allocated 160 more that whatever it had, next it did not deallocate, in the 3rd example it apperently did not need more, therefor memory_get_usage() - $s = 0 Commented Aug 26, 2015 at 10:10
  • Did not get your answer. Could you please clarify a little? Commented Aug 26, 2015 at 11:20
  • memory_get_usage does not report mem-usage per variable or similar, but per (de-)allocated "chunk" of mem. Say php starts with 0.5Mb, then you do some stuff and it decides it needs 1Mb more, you will only be able to measure the change from 0.5Mb to 1.5Mb because it measures memory allocation. That is in contrast to memory per variable usage. It will almost allways allocate more than it needs right now, since allocating memory is not free, and it tries to keep the calls to alloc at a reasonable minimum. Commented Aug 26, 2015 at 11:55

2 Answers 2

1

Use memory_get_usage(true) every time, you will get your answer.

If true is not passed , it returns memory used by emalloc. You can read more about that on Fucntion's Definition

Sign up to request clarification or add additional context in comments.

2 Comments

Using memory_get_usage(true) returns 0,0,0 respectively. As you understand, it cannot be the REAL size.
That is really weird behavior without passing true, but with true PHP will give you information how much memory it has been allocated by system. And 0 means PHP already has enough memory to assign a variable true. for some big variable , it will return new set allocated.
1

I found these functions in an internet search a while ago and use them in my scripts (sorry original author, lost the link to give you credit here). You could include the functions in your code to make things more modular and easier to read, then call the memoryusage function whenever you need it, eg...

// This function converts the number of bytes sent to it to kb, mb, gb, tb, pb as appropriate based on the size of memory (powers of 1024)
function convert($size)
{
    $unit=array('b','kb','mb','gb','tb','pb');
    return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}

// This function gets current PHP memory use and returns an approximate (rounded) figure back in: bytes, kb, mb, gb, tb, or pb as appropriate
function memoryusage() {
    return convert(memory_get_usage(true));
}

....

// This is me wanting to know how much memory is being used at this point of the script
$memuse = memoryusage();

...

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.