Skip to content

Instantly share code, notes, and snippets.

@sandyUni
Created February 6, 2014 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandyUni/8847661 to your computer and use it in GitHub Desktop.
Save sandyUni/8847661 to your computer and use it in GitHub Desktop.
测试使用static变量和自身变量时实例化变量后的内存消耗,实验证明,Static会消耗很少的内存,前提是实例化的类中数据足够多,以至于类本身需要的内存可以忽略
*/
/**
* test memory comsuming
*/
class base1{
public $a;
public $b;
public $c;
public $d;
}
class base2{
public static $a;
public static $b;
public static $c;
public static $d;
}
class sonfrombase1 extends base1{
public function __construct($b) {
$this->a=$b;
$this->b=$b;
$this->c=$b;
$this->d=$b;
}
}
class sonfrombase2 extends base2{
public function __construct($b) {
parent::$a=$b;
parent::$b=$b;
parent::$c=$b;
parent::$d=$b;
}
}
echo "usenow: ";
echo memory_get_usage();
echo "<br>";
$usest= memory_get_usage();
$son1s=array();
for($i=0;$i<10000;$i++){
$son1s[$i]=new sonfrombase1(array(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9));
}
usleep(5000000);
$useend= memory_get_usage();
$son1use=$useend-$usest;
foreach ($son1s as $value) {
unset($value);
}
echo "use then: ";
echo memory_get_usage();
echo "<br>";
$usest= memory_get_usage();
$son2s=array();
for($i=0;$i<10000;$i++){
$son2s[$i]=new sonfrombase2(array(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9));
}
usleep(5000000);
$useend= memory_get_usage();
$son2use=$useend-$usest;
foreach ($son2s as $value) {
unset($value);
}
echo "use last: ";
echo memory_get_usage();
echo "<br>";
echo 'local use: ';
echo $son1use;
echo "<br>";
echo 'static use: ';
echo $son2use;
echo "<br>";
echo "local Use more than static Use up to:";
echo ($son1use-$son2use)/$son2use;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment