使用脚本语言最大的好
当然,我们可以按自己
不过在PHP里,至少
问题症状
如果两个对象之间存在
有些糊涂了?我们来看
<?php
class Foo {
function __construc t()
{
$this->bar = new Bar($this) ;
}
}
class Bar {
function __construc t($foo = null)
{
$this->foo = $foo;
}
}
while (true) {
$foo = new Foo();
unset($foo );
echo number_for mat(memory _get_usage ()) . "\n";
}
?>
运行这段代码,你会看
...
33,551,616
33,551,976
33,552,336
33,552,696
PHP Fatal error: Allowed memory size of 33554432 bytes exhausted
(tried to allocate 16 bytes) in memleak.ph p on line 17
对大部分PHP程序员
可如果你在一个长期运
Userland解决 方案
虽然有些乏味、不优雅
这个方案在释放对象前
以下是“修复后”的代
<?php
class Foo {
function __construc t()
{
$this->bar = new Bar($this) ;
}
function __destruct ()
{
unset($thi s->bar);
}
}
class Bar {
function __construc t($foo = null)
{
$this->foo = $foo;
}
}
while (true) {
$foo = new Foo();
$foo->__de struct();
unset($foo );
echo number_for mat(memory _get_usage ()) . "\n";
}
?>
注意那个新增的 Foo::__des
PHP内核解决方案?
为什么会有内存溢出的
在 $bar 中引用 $foo 的引用计数不会因为父
这里确实可以看出我的
在前面提到的 bugs.php.n
与其改变垃圾回收的过
也许PHP内核开发者
更新:Martin Fjordvald 在评论中提到了一个由