|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76982 memory leak declaring closure in included file
Submitted: 2018-10-08 02:40 UTC Modified: 2021-03-01 10:44 UTC
Avg. Score:4.6 ± 0.6
Reproduced:72 of 76 (94.7%)
Same Version:15 (20.8%)
Same OS:50 (69.4%)
From: ryan dot brothers at gmail dot com Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: 7.2.10 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: ryan dot brothers at gmail dot com
New email:
PHP Version: OS:


 [2018-10-08 02:40 UTC] ryan dot brothers at gmail dot com
I am running PHP 7.2.10 on CentOS 7.  If I run the below script file1.php, the memory usage gradually increases until it hits the memory limit.  The memory usage should remain relatively constant.  It's related to declaring the closure in file2.php, as if I remove the closure, then the memory usage stays constant.

Also, the memory usage stays constant in PHP 5.6, so the issue is perhaps only in PHP 7 and later.

Test script:
for ($i = 0; $i < 1000000; $i++)
	echo $i.' - '.number_format(memory_get_usage())."\n";


$test_function = function()

Expected result:
Memory usage stays constant.

Actual result:
Memory usage increases until it hits the memory limit.


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-10-09 12:07 UTC]
-Status: Open +Status: Verified
 [2018-10-09 12:07 UTC]
This also happens on current master.  It works fine if file2.php
is inlined.  Neither a debug build nor valgrind
leak, though.
 [2018-10-09 13:21 UTC] ryan dot brothers at gmail dot com
Thanks for confirming.  If it helps, the same problem also happens if you put "return;" as the first line of file2.php like the below, so it seems related to compiling perhaps, rather than executing.


$test_function = function()
 [2019-05-27 21:28 UTC] tyler dot bannister at remote-learner dot net
I'm seeing the same problem even when I don't use a closure and instead just assign a variable.  I also saw the same problem, but to a lesser extent, with a print command.  I filed a report for that in Bug #78076, but it seems like there may be a common problem related to repeated requires/includes?
 [2019-06-27 20:23 UTC] goetas at gmail dot com
I manage to reproduce consistently the issue on all PHP 7.x versions contains the repo to reproduce the issue is one build that shows the issue on Travis

Discovered while trying to solve
 [2019-06-28 15:10 UTC] goetas at gmail dot com
This bug is pretty serious when integrating the symfony dependency injection container in reactphp, phppm or swoole applications.

This shows the leak in the default configuration of the symfony dependency injection
 [2021-02-19 17:16 UTC]
The following pull request has been associated:

Patch Name: Reference dynamic functions through dynamic_defs
On GitHub:
 [2021-03-01 10:44 UTC]
This leak is partially fixed in PHP 8.1. There's still a memory leak, but it's a constant leak of ~250 bytes per overwritten closure now.
 [2021-04-09 21:43 UTC] zoohie at gmail dot com
Problem occurs not only with closures but also with anonymous classes.
 [2021-10-06 09:23 UTC] michael dot vorisek at email dot cz
if (!class_exists(Test::class)) {
    class Test {

is leaking too, even if the the if condition is satisfied only once, test . Seems to be fixed in PHP 8.1, but only when opcache is enabled (which is not by default for CLI).
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 21 19:01:32 2024 UTC