我最近开始优化我的Collection类,发现内存泄漏。代码的例子:
class anObject {}
$array = array();
for ($i=0; $i < 5000; $i++) {
$obj = new anObject();
for ($j = 0; $j <5; $j++) {
$obj->{'test'.$j} = $j;
}
$array[] = $obj;
}
echo memory_get_usage();
// 3548328
unset($array);
class anObjetWithSetter {
public function __set($key, $value) {
$this->{$key} = $value;
}
}
$array = array();
for($i=0; $i < 5000; $i++) {
$obj = new anObjetWithSetter();
for ($j = 0; $j <5; $j++) {
$obj->{'test'.$j} = $j;
}
$array[] = $obj;
}
echo memory_get_usage();
// 5708440
任何人都可以对此内存泄漏有所了解吗?