[php-src] Issue #12773: Possible memory leak when using generators

From: Date: Fri, 24 Nov 2023 16:38:05 +0000
Subject: [php-src] Issue #12773: Possible memory leak when using generators
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-245886@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/12773 Author: flip111 ### Description I believe this is a bug, perhaps i made some mistake but anyway i thought it was worth reporting. The code is somewhat long and likely the algorithm can be improved and the code structure (like using globals) can also be improved. Though i think that is irrelevant for the possible bug. I expect that the part of the code that should be understood is about 20 lines. The only function that WRITES to a global is the go() function. The globals that are being written: $best_time (int/float), $match_count (int), $last_seen_time (int), $abort (boolean/string). --- [bug.php.txt](https://github.com/php/php-src/files/13460926/bug.php.txt) My reasoning why i think it's a bug. 1. On line 889 a rather low memory usage is reported (around half a megabyte). 2. From then on the following variables are being written to: $generator, $states, $last, $best_solution and all the globals that go() used. 3. On line 920 memory usage is reported. This increase in memory can not come from the code that starts after line 928. 4. All variables being written to between line 889 and 919 are either unset or made sure that are of a type that does not use much memory (with exception of $abort, but it has a known value of type string). 5. I believe gc_collect_cycles() is blocking like any other php function that not specifically declares itself to be non-blocking, but just in case i also tried to give it some extra wait time (uncomment line 921 to 924) The reported memory usage: * PHP "used memory". About 129MB * PHP total memory allocated from system. 32GB on my system. HTOP reports * 32GB virtual memory use (seems to be the same number as PHP reports as total memory allocated from the system) * 25GB resident memory use (not sure how to get this number from within the PHP script) Somewhere i think 25GB of memory is leaking because i don't see any variable holding onto that memory. ### PHP Version PHP 8.2.10-2ubuntu1 (cli) (built: Sep 5 2023 14:37:47) (NTS) ### Operating System Ubuntu 23.10

« previous php.bugs (#245886) next »