I tried to compare the constants and variables used in memory, but found a strange phenomenon.
test code:
<?php
var_dump( memory_get_usage() ); // int(129100)
// I don't understand: I don't do someting but why there has the changed
var_dump( memory_get_usage() ); // int(129156)
var_dump( memory_get_usage() ); // int(129156)
define('hello', 'WORLD');
// why here hasn't changed
var_dump( memory_get_usage() ); // int(129156)
$hello = 'WORLD';
var_dump( memory_get_usage() ); // int(129304)
?>
First: PHP has a lot of memory leaks. This means, it does allocate memory somewhere, but doesn’t free it. So don’t be afraid seeing such effects. But this time it’s probably something else:
Look at this example:
The output will be like:
You see that it changes after the first call to “echo”, which must be allocating memory for the output buffer or something like that. Maybe its internally using printf(buffer, “…”, …) to generate the string thats send to the standard output. This string is reused in later calls and just resized to a longer buffer if needed.
Your call to var_dump indirectly uses the echo function and probably also uses some internal buffer, thats allocated at the first call.
Solution: To measure things more accurate you should call each output function once before your “measurements” or never call them before the final output of your results.