php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81215 Possible memory leak with closure nested \array_filter()
Submitted: 2021-07-01 12:37 UTC Modified: 2021-07-02 13:35 UTC
From: berliozdavid at gmail dot com Assigned:
Status: Suspended Package: Performance problem
PHP Version: 8.0.8 OS: Fedora/Linux
Private report: No CVE-ID: None
 [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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-01 12:42 UTC] berliozdavid at gmail dot com
Using static closure leads to memory leak too:

$f = static function ($array) {
    return \array_filter($array, fn($item) => !is_null($item));
};
 [2021-07-01 12:55 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2021-07-01 12:55 UTC] nikic@php.net
I can't reproduce this, memory usage is stable for me. Tested with and without opcache.

Do you have any interesting extensions like xdebug loaded?
 [2021-07-01 13:01 UTC] berliozdavid at gmail dot com
-Status: Feedback +Status: Open
 [2021-07-01 13:01 UTC] berliozdavid at gmail dot com
[PHP Modules]
apcu
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
igbinary
imagick
inotify
intl
json
libxml
mbstring
memcached
mongodb
msgpack
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
ssh2
standard
swoole
sysvmsg
sysvsem
sysvshm
tokenizer
xhprof
xml
xmlreader
xmlwriter
xsl
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache
 [2021-07-01 13:20 UTC] nikic@php.net
Thanks! Some of those (e.g. xhprof, swoole) look potentially problematic in terms of modifying engine behavior. Can you please check whether you still see when using "php -n" (i.e. without ini file)?
 [2021-07-01 13:42 UTC] berliozdavid at gmail dot com
I can confirm that php -n is not affected.

So far I've tested separately

php -n -dextension=swoole.so

opcache
xhprof

No memory leak

I can test with a set of common modules if you provide me a list.
 [2021-07-02 05:42 UTC] berliozdavid at gmail dot com
On my rig the culprits are opcache + xhprof

php -n -dzend_extension=opcache -dopcache.enable=1 -dopcache.enable_cli=1 -dextension=xhprof.so array_filter_short.php
 [2021-07-02 05:47 UTC] berliozdavid at gmail dot com
I've installed both exts with PHP80 and PHP74

php74 -n -dzend_extension=opcache -dopcache.enable=1 -dopcache.enable_cli=1 -dextension=xhprof.so array_filter_short.php --> OK no increase

php80 -n -dzend_extension=opcache -dopcache.enable=1 -dopcache.enable_cli=1 -dextension=xhprof.so array_filter_short.php --> KO increase
 [2021-07-02 08:15 UTC] nikic@php.net
-Status: Open +Status: Suspended
 [2021-07-02 08:15 UTC] nikic@php.net
I'd suggest reporting the issue to the xhprof extension. Most likely the problem is on their side, and if not they can provide more information on the issue (possibly there is some bad interaction between the observer API and closures? I have no idea what xhprof uses on PHP 8.)
 [2021-07-02 13:35 UTC] berliozdavid at gmail dot com
cross ref https://github.com/phacility/xhprof/issues/105
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 13 05:01:27 2024 UTC