|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2021-07-01 12:37 UTC] berliozdavid at gmail dot com
Description:
------------
With PHP80 using \array_filter() in a closure can lead to memory leak.
PHP74 is not affected.
Test script:
---------------
<?php
namespace Somewhere\On\Earth;
class Foo {
function leaking(array $array) {
$f = function ($array) {
return \array_filter($array, fn($item) => !is_null($item));
};
for (;;) {
$clone = (array) $array;
$res = $f($clone);
printf("memory: %d\n", \memory_get_usage());
unset($clone, $res);
}
}
function run() {
$array = array_combine(range(1, 1000, 2), array_fill(1, 500, null));
$array += array_combine(range(2, 1000, 2), array_fill(1, 500, true));
$this->leaking($array);
}
}
(new Foo())->run();
Expected result:
----------------
stable memory value.
Actual result:
--------------
increasing memory value.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 14:00:01 2025 UTC |
Using static closure leads to memory leak too: $f = static function ($array) { return \array_filter($array, fn($item) => !is_null($item)); };