php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80140 Including a file multiple times with anonymous class is leaking memory
Submitted: 2020-09-22 21:07 UTC Modified: 2020-09-22 21:27 UTC
From: mvorisek at mvorisek dot cz Assigned:
Status: Duplicate Package: Scripting Engine problem
PHP Version: 7.4.10 OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
2 + 14 = ?
Subscribe to this entry?

 
 [2020-09-22 21:07 UTC] mvorisek at mvorisek dot cz
Description:
------------
see https://3v4l.org/nUFqX

including file multiple times is very common usecase and memory should not increase

the issue is present when the included file contains an anonymous class, but the class should be not recreated again and again

Test script:
---------------
if (isset($fromRequire)) {
    // return; // leaking even if code below is never executed
    $class = new class() extends \stdClass {};

    return;
}

for ($i = 0; $i < 10 * 1000; $i++) {
    $fromRequire = true;
    require __FILE__;

    if (($i % (1000)) === 0) {
        gc_collect_cycles();
        echo round(memory_get_usage() / (1024 * 1024), 3) . " MiB\n";
    }
}

Expected result:
----------------
reported memory usage should stay below 1 MiB

Actual result:
--------------
0.37 MiB
1.152 MiB
1.973 MiB
2.872 MiB
3.614 MiB
4.67 MiB
5.412 MiB
6.155 MiB
6.898 MiB
8.265 MiB

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-09-22 21:24 UTC] mvorisek at mvorisek dot cz
Even function declaration included multiple times is leaking memory:
```
return;
function xxx() {}
```
https://3v4l.org/NOejV
 [2020-09-22 21:24 UTC] nikic@php.net
-Status: Open +Status: Duplicate
 [2020-09-22 21:27 UTC] nikic@php.net
Duplicate of bug #76982. Also last comment on bug #79519.
 
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed Nov 29 23:01:27 2023 UTC