从对象数组导出大量文件时,我遇到内存泄漏问题.简化代码如下所示:
class Test_Class
{
private $a = null;
public function __construct($a = null)
{
$this->a = $a;
}
public function __destruct()
{
unset($this->a);
}
}
print 'Memory before: '.memory_get_usage(1).' <br>'; // 262 144
$a = [];
for ($i=0; $i<600000; $i++)
$a[] = new Test_Class($i);
print 'Memory after create: '.memory_get_usage(1).' <br>'; // 129 761 280
for($i=0; $i < count($a); $i++)
unset($a[$i]);
unset($a);
print 'Memory after: '.memory_get_usage(1).' <br>'; // 35 389 440
Run Code Online (Sandbox Code Playgroud)
在下一次迭代中,内存仍然结束.知道如何释放占用的内存吗?
PS我尝试unset()/赋值null和gc_collect_cycles(),没有一个方法允许我释放对象数组占用的内存