测试一:
class Entity { }
$entities = array();
echo "START: ". number_format(memory_get_usage()) . "\n";
for($i = 0; $i < 100000; ++$i)
{
$entities[] = new Entity();
}
echo "BEFORE UNSET: ". number_format(memory_get_usage()) . "\n";
unset($entities);
echo "AFTER UNSET: ". number_format(memory_get_usage()) . "\n";
输出:
START: 631,664
BEFORE UNSET:44,404,904
AFTER UNSET: 8,954,568
测试二:
class Entity { }
$entities = array();
echo "START: ". number_format(memory_get_usage()) . "\n";
for($i = 0; $i < 100000; ++$i)
{
$entity = new Entity();
$entities[] = &$entity;
}
echo "BEFORE UNSET: ". number_format(memory_get_usage()) . "\n";
unset($entities);
echo "AFTER UNSET: ". number_format(memory_get_usage()) . "\n";
输出:
START: 631,664
BEFORE UNSET: 10,480,480
AFTER UNSET: 631,752
在我遇到内存限制问题后,我玩了一下......
所以在那之后我想知道垃圾收集是如何工作的:
- 为什么测试一比测试二需要更多的内存?
- 为什么在测试一中取消设置数组后 php 会保留内存?
谢谢戴夫