Reputation: 25580
It looks like in PHP it requires about 213 bytes to store one integer, is it true? Okay, please take a look on the next code:
$N = 10000;
echo memory_get_usage()."\n";
$v = array();
for($i = 0; $i < $N; $i++) {
$v[] = $i;
}
echo memory_get_usage()."\n";
unset($v);
echo memory_get_usage()."\n";
Output is next:
641784
2773768
642056
So, the difference is 2773768 - 641784 = 2131984 byte, or 213 byte per integer. why so much? 4 bytes is more than enough.
Upvotes: 1
Views: 175
Reputation: 212522
4 bytes is only enough if you simply store an integer value somewhere in memory, without making any allowance for the fact that it is a variable which needs a datatype identification, flags to indicate if there are any other references to that variable, the name of that variable, etc. all of which require additional memory.
PHP stores the value in a zval* so there's all the additional bytes used to store the zval details in addition to the actual value.
Upvotes: 3