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
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.
Password:
Status:
Package:
Bug Type:
Summary:
From: mvorisek at mvorisek dot cz
New email:
PHP Version: OS:

 

 [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-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 22:01:29 2024 UTC