PHP:在循环内或外实例化类在占用内存上有很大的区别!

2012年12月23日 发表评论 阅读评论
class ABC{
    public $numBox = array();
    public function ddd(){
            for($i=1; $i<=600; $i++){
                    $this->numBox[] = mt_rand(1,1000000);
           }
                echo 'Now memory_get_usage: '.memory_get_usage().'';
        }

}
$instance = new ABC();
for($i =0; $i< 50 ; $i++){
     $instance->ddd();
}

这是test1.php的运行结果
Now memory_get_usage: 422728
Now memory_get_usage: 474824
Now memory_get_usage: 522824
Now memory_get_usage: 579024
Now memory_get_usage: 627024
Now memory_get_usage: 675024
Now memory_get_usage: 739408
Now memory_get_usage: 787416
Now memory_get_usage: 835416
Now memory_get_usage: 883416
Now memory_get_usage: 931416
Now memory_get_usage: 979416
Now memory_get_usage: 1027416
………………

class ABC{
    public $numBox = array();
    public function ddd(){
            for($i=1; $i<=600; $i++){
                    $this->numBox[] = mt_rand(1,1000000);
           }
                echo 'Now memory_get_usage: '.memory_get_usage().'';
        }

}
for($i =0; $i< 50 ; $i++){
     $instance = new ABC();
     $instance->ddd();
}

这是test2.php的运行结果
Now memory_get_usage: 422728
Now memory_get_usage: 422712
Now memory_get_usage: 422712
Now memory_get_usage: 422712
Now memory_get_usage: 422712
Now memory_get_usage: 422712
Now memory_get_usage: 422712
Now memory_get_usage: 422712
Now memory_get_usage: 422712
Now memory_get_usage: 422712
Now memory_get_usage: 422712
Now memory_get_usage: 422712
Now memory_get_usage: 422712
Now memory_get_usage: 422712
Now memory_get_usage: 422712
Now memory_get_usage: 422712
Now memory_get_usage: 422712
……………………….


转载请注明来自:[MSN Spaces]http://msn.shandian.biz/482.html

  1. 本文目前尚无任何评论.