php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80777 Memory Leak on function() in include
Submitted: 2021-02-19 17:07 UTC Modified: 2021-02-19 17:16 UTC
From: php dot net at mailt dot so Assigned:
Status: Duplicate Package: opcache
PHP Version: Irrelevant OS: Any/Debian
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:
4 + 39 = ?
Subscribe to this entry?

 
 [2021-02-19 17:07 UTC] php dot net at mailt dot so
Description:
------------
Earliest tested version: 7.3.7, also tested: 7.4.15 (Debian package) 8.0.2 (Compiled from src)
Propably all versions affected.

Defining a function in an include file seems to break/disable some kind of cache cleaning and results in a memory leak.
In most cases this might not hurt, since it only occurs once. I found this on an run-time editable script where the include file gets reloaded on every loop and after years of using it this way, it suddently broke and instead of a few kbyte of ram it crashed after a day reaching the 125mb limit.
After searching and fiddling with the prior changes i made i reduced the code to a minimal test for this specific problem.


Test script:
---------------
main.php:
<?php
function checkmem($name = "") { global $oldmem; $newmem = memory_get_usage(false); if ($newmem > $oldmem) { echo "\n".$oldmem." -> ".$newmem." (".($newmem-$oldmem).") Checkpoint $name\n"; $oldmem = $newmem; }
}

while (!usleep(10000)) {
checkmem("Preinclude");
include("inc.php");
checkmem("Postinclude");
}
?>

inc.php:
<?php
checkmem("inner begin");
if (1 == 0) function test() {}
checkmem("inner end");
?>

Expected result:
----------------
The memory size shouldn't increase unlimited (The code is never reached, the include is left successfully, etc..)
The result is the same whether or not the function exists.

Actual result:
--------------
php main.php
 -> 394136 (394136) Checkpoint Preinclude
394136 -> 395256 (1120) Checkpoint inner begin
395256 -> 460792 (65536) Checkpoint inner begin
460792 -> 526328 (65536) Checkpoint inner begin
526328 -> 591864 (65536) Checkpoint inner begin
.... until OOM

Just re-including a file unlimited times is no problem until a user defined function is declared within. Then some kind of optimization seems to fail and the used memory get's bigger and bigger. Without the user defined function definition in the include, the memory amount is almost unchanged even when the file is changed during runtime (and reincluded ...)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-19 17:16 UTC] nikic@php.net
-Status: Open +Status: Duplicate
 [2021-02-19 17:16 UTC] nikic@php.net
Duplicate of bug #76982.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Sep 27 15:05:53 2022 UTC