Bug #68132 [NEW]: Memory leakage while using anonymous functions

From: Date: Thu, 02 Oct 2014 13:55:40 +0000
Subject: Bug #68132 [NEW]: Memory leakage while using anonymous functions
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-187791@lists.php.net to get a copy of this message
From: nikola at petkanski dot com Operating system: Linux PHP version: 5.4.33 Package: Performance problem Bug Type: Bug Bug description:Memory leakage while using anonymous functions Description: ------------ This bug seem like a duplicate of an earlier one that got marked as fixed: - https://bugs.php.net/bug.php?id=60139 It has been tried on the following configurations and the problem persists: PHP 5.4.32 (cli) (built: Aug 22 2014 07:07:38) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies PHP 5.5.9-1ubuntu4.4 (cli) (built: Sep 4 2014 06:56:34) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans For convenience I'm gonna copy the description of the original bug as it seem relevant and I guess the problem is the same: ----- 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) <?php class Foo { public $x; public function __construct() { $this->x = function() {}; } } echo memory_get_usage(), "\n"; for ($i = 0; $i < 100000; ++$i) { new Foo; } gc_collect_cycles(); echo memory_get_usage(), "\n"; ?> # Script 2 (leaks) <?php class Foo { public $x; public function __construct() { $self = $this; $this->x = function() use ($self) {}; } } echo memory_get_usage(), "\n"; for ($i = 0; $i < 100000; ++$i) { new Foo; } gc_collect_cycles(); echo memory_get_usage(), "\n"; ?> # Script 3 (does not 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 approximately the same: nikola@nikola-450G1:~$ php script3.php 237944 238152 Actual result: -------------- Objects are never freed and memory usage increases: nikola@nikola-450G1:~$ php script1.php 237296 1220536 nikola@nikola-450G1:~$ php script2.php 237848 1221256 -- Edit bug report at https://bugs.php.net/bug.php?id=68132&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68132&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68132&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68132&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=68132&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=68132&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=68132&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=68132&r=needscript Try newer version: https://bugs.php.net/fix.php?id=68132&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=68132&r=support Expected behavior: https://bugs.php.net/fix.php?id=68132&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=68132&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=68132&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=68132&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68132&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=68132&r=dst IIS Stability: https://bugs.php.net/fix.php?id=68132&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=68132&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=68132&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=68132&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=68132&r=mysqlcfg

« previous php.bugs (#187791) next »