From:
Operating system:
PHP version: 5.4.0beta2
Package: Performance problem
Bug Type: Bug
Bug description:Anonymous functions create cycles not detected by the GC
Description:
------------
In the following Script 1 and Script 2, the created objects are not
destructed until the engine shutdowns because of a reference cycle.
The objects hold a reference to an anonymous function that itself hold a
reference to the object.
It seems that the garbage collector is unable to break the cycle.
Script 3 doesn't leak memory because the anonymous function doesn't hold a
reference to the object.
Test script:
---------------
# Script 1 (leaks with php5.4)
<?php
class Foo
{
public $x;
public function __construct() {
$this->x = function() {};
}
}
echo memory_get_usage(), "\n";
for ($i = 0; $i < 1000; ++$i) {
new Foo;
}
gc_collect_cycles();
echo memory_get_usage(), "\n";
?>
# Script 2 (leaks with php5.3)
<?php
class Foo
{
public $x;
public function __construct() {
$self = $this;
$this->x = function() use ($self) {};
}
}
echo memory_get_usage(), "\n";
for ($i = 0; $i < 1000; ++$i) {
new Foo;
}
gc_collect_cycles();
echo memory_get_usage(), "\n";
?>
# Script 3 (no leak)
<?php
class Foo
{
public $x;
public function __construct() {
$this->x = get_fun();
}
}
function get_fun() {
return function() {};
}
echo memory_get_usage(), "\n";
for ($i = 0; $i < 1000; ++$i) {
new Foo;
}
gc_collect_cycles();
echo memory_get_usage(), "\n";
?>
Expected result:
----------------
Memory usage before and after is approximatel the same:
121040
121152
Actual result:
--------------
Objects are never freed and memory usage increases:
120724
417640
--
Edit bug report at https://bugs.php.net/bug.php?id=60139&edit=1
--
Try a snapshot (PHP 5.4):
https://bugs.php.net/fix.php?id=60139&r=trysnapshot54
Try a snapshot (PHP 5.3):
https://bugs.php.net/fix.php?id=60139&r=trysnapshot53
Try a snapshot (trunk):
https://bugs.php.net/fix.php?id=60139&r=trysnapshottrunk
Fixed in SVN:
https://bugs.php.net/fix.php?id=60139&r=fixed
Fixed in SVN and need be documented:
https://bugs.php.net/fix.php?id=60139&r=needdocs
Fixed in release:
https://bugs.php.net/fix.php?id=60139&r=alreadyfixed
Need backtrace:
https://bugs.php.net/fix.php?id=60139&r=needtrace
Need Reproduce Script:
https://bugs.php.net/fix.php?id=60139&r=needscript
Try newer version:
https://bugs.php.net/fix.php?id=60139&r=oldversion
Not developer issue:
https://bugs.php.net/fix.php?id=60139&r=support
Expected behavior:
https://bugs.php.net/fix.php?id=60139&r=notwrong
Not enough info:
https://bugs.php.net/fix.php?id=60139&r=notenoughinfo
Submitted twice:
https://bugs.php.net/fix.php?id=60139&r=submittedtwice
register_globals:
https://bugs.php.net/fix.php?id=60139&r=globals
PHP 4 support discontinued:
https://bugs.php.net/fix.php?id=60139&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=60139&r=dst
IIS Stability:
https://bugs.php.net/fix.php?id=60139&r=isapi
Install GNU Sed:
https://bugs.php.net/fix.php?id=60139&r=gnused
Floating point limitations:
https://bugs.php.net/fix.php?id=60139&r=float
No Zend Extensions:
https://bugs.php.net/fix.php?id=60139&r=nozend
MySQL Configuration Error:
https://bugs.php.net/fix.php?id=60139&r=mysqlcfg