Why does a simple PHP unset() memory test utilize twice the amount of memory in PHP 5.3?
I was testing out whether unset() affects memory while the script is running, to see if unset() or other known method, $var=null is more efficient. unset() did affect memory, but since I tested it out on two different virtualhosts, I wondered why does one take more or less twice the amount of memory for the same script? I'm guessing the ans开发者_开发问答wer is something simple, but it escapes me at the moment. Script itself is below:
<?php
$init=memory_get_usage();
$test=array();
for($i=0;$i<=100000;$i++){
$test[$i]=rand(0,10000000);
}
echo 'MEMORY CHANGE: '.((memory_get_usage()-$init)/1024/1024).'MB<br/>';
for($i=0;$i<=100000;$i++){
unset($test[$i]);
}
echo 'MEMORY CHANGE: '.((memory_get_usage()-$init)/1024/1024).'MB<br/>';
//output on PHP 3.2.5 virtualhost:
//MEMORY CHANGE: 6.98558807373MB
//MEMORY CHANGE: 0.500259399414MB
//output on PHP 5.3.5 virtualhost
//MEMORY CHANGE: 13.970664978MB
//MEMORY CHANGE: 1.00063323975MB
?>
Thanks!
PHP 3.2.5? That's so old it doesn't even reach the stone age. PHP's guts underwent a total re-write with the Zend engine, so even though the language itself stayed relatively the same, you're comparing two different environments.
But in case that version number is a typo, then possibly it's a 32bit v.s. 64bit host, which doubles the size of ints and could account for your purported 2x difference.
精彩评论