|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
Patchesgc-closure2.diff (last revision 2011-10-31 11:04 UTC by dmitry@php.net)gc-closure.diff (last revision 2011-10-31 11:04 UTC by dmitry@php.net) bug60139.php5.4.diff (last revision 2011-10-29 10:23 UTC by arnaud dot lb at gmail dot com) test2.phpt (last revision 2011-10-28 14:24 UTC by arnaud dot lb at gmail dot com) test1.phpt (last revision 2011-10-28 14:23 UTC by arnaud dot lb at gmail dot com) Pull RequestsHistoryAllCommentsChangesGit/SVN commits
[2011-10-31 08:19 UTC] dmitry@php.net
-Assigned To:
+Assigned To: dmitry
[2011-10-31 08:44 UTC] dmitry@php.net
[2011-10-31 11:04 UTC] dmitry@php.net
[2011-10-31 11:04 UTC] dmitry@php.net
[2011-11-02 06:31 UTC] dmitry@php.net
[2011-11-02 06:31 UTC] dmitry@php.net
[2011-11-02 06:31 UTC] dmitry@php.net
-Status: Assigned
+Status: Closed
[2012-04-18 09:48 UTC] laruence@php.net
[2012-07-24 23:39 UTC] rasmus@php.net
[2013-11-17 09:35 UTC] laruence@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 13 08:00:02 2025 UTC |
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