2

I know, this question might have been asked a few times before, but I've read all the similar question here and all the answers and still don't understand. So, I have a single variable declaration in my script:

$a = 255;

How much memory will this variable require? I've read this excellent article, which explains how much memory will be allocated for internal PHP structures (like _zval_struct, _zval_gc_info, _zend_mm_block_info). The result is 48 bytes. But on my machine I get 168 bytes. Where do they come from? I'm getting this number by calling memory_get_usage() before and after the declaration.

I'm running PHP 5.5.18 (64-bit) on Mac OS X.

Thanks in advance.

Alexander Guz
  • 1,334
  • 12
  • 31
  • 32-bit PHP or 64-bit PHP? But 28bytes for 32-bit and 56 bytes for 64-bit PHP – Mark Baker Jan 27 '15 at 15:25
  • http://stackoverflow.com/questions/5972170/what-is-the-overhead-of-using-php-int – oshell Jan 27 '15 at 15:26
  • Note that PHP memory is not allocated in bytes, but in blocks, so you're not likely to get an exact figure simply by measuring memory usage before and after the assignment – Mark Baker Jan 27 '15 at 15:32
  • @MarkBaker The `memory_get_usage(true)` function (note the `true` param) will return the size of all blocks. – Alexander Guz Jan 27 '15 at 15:34
  • @Escobear I've read this post. It doesn't explain why I get 168 bytes. – Alexander Guz Jan 27 '15 at 15:37
  • From Mark's comment. @GuzAlexander "Note that PHP memory is not allocated in bytes, but in blocks" – geggleto Jan 27 '15 at 15:59
  • I'm guessing you are talking about the difference between what `memory_get_usage()` returns _before_ and _after_ `$a = 255;`, right? In that case `memory_get_usage()` is completely useless. On my machine it reports 136 bytes with _no_ code between the two calls. It also reports a constant 296 extra bytes no matter how many times I repeat a char (`str_repeat`) _if_ I repeat it more than 100 times. For less than that it's almost random. – Sergiu Paraschiv Jan 27 '15 at 16:02
  • Besides the fact that it reports _blocks_, not _bytes_, it also reports memory used by allocations done by language constructs and functions "inside" of them. Another "curiosity" is that `echo memory_get_usage() - memory_get_usage()` outputs "-48". So a call to `memory_get_usage` allocates memory. – Sergiu Paraschiv Jan 27 '15 at 16:03
  • @SergiuParaschiv Thanks for the comments. So, I'm getting the wrong number with `memory_get_usage()`. The question is still open: how to get the exact number? – Alexander Guz Jan 28 '15 at 07:06
  • 1
    Then you need an _external_ memory profiler. XDebug used to have that but they dropped it. There's a php-memory-profiler lib on github that works better: https://github.com/arnaud-lb/php-memory-profiler – Sergiu Paraschiv Jan 28 '15 at 08:57

1 Answers1

0

So, thanks to Sergiu Paraschiv's comment, I installed memprof and made a small research on the topic. Here is what I got on my Mac with PHP 5.5.18 (64-bit):

<?php
memprof_enable();

$a = 255;           // +104 (+32 for smth initial?), total: 136
$b = 255;           // +104, total: 240
$c = 255.5;         // +104, total: 344
$d = 'h';           // +104, total: 448
$e = [];            // +176, total: 624
$f = new stdClass;  // +136, total: 760
$g = $f;            // +72, total: 832
$h = $g;            // +72, total: 904
$i = $a;            // +72, total: 976
$j = &$i;           // +104, total: 1080
$k = &$j;           // +72, total: 1152  why not 104?
$l = &$h;           // +104, total: 1256
$m = null;          // +104, total: 1360
$n = true;          // +104, total: 1464

print_r(memprof_dump_array());

Array (
[memory_size] => 1464
[blocks_count] => 27
[memory_size_inclusive] => 1464
[blocks_count_inclusive] => 27
[calls] => 1
[called_functions] => Array
    (
        [memprof_dump_array] => Array
            (
                [memory_size] => 0
                [blocks_count] => 0
                [memory_size_inclusive] => 0
                [blocks_count_inclusive] => 0
                [calls] => 1
                [called_functions] => Array
                    (
                    )

            )

    )
)
Alexander Guz
  • 1,334
  • 12
  • 31