-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Garbage collector not cleaning roots / leaking memory #9239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I don't see a memory leak here. While the script is executing |
Perhaps I'm missing something obvious but that's not what's going on if you look carefully at the output. The root buffer count keeps increasing between ff() calls and the total memory as well. What you're saying is exactly what I'd expect to happen but that's NOT THE CASE. If the total number of objects marked as root stored in the $arr is lower than 10 000, the memory is properly freed. I'll provide an example output for that as well. Please take a look at the issue once again. |
Yes, of course, since you are creating 10,000,000 objects in |
I understand that the root buffer grows in size but the gc_status()["roots"] number is supposed to represent current number of possible roots, not the size of the buffer. And if you look at the output of third (after gc_collect) and fourth call to ff, the number of reported roots grew from 0 to 700 000+ which corresponds to the fact that nothing was actually freed. The total memory usage also decreased only slightly after gc_collect and is the highest after fourth call. |
Ah, I see, if the documentation is outdated and it's no longer true that the gc is triggered for every 10 000 roots, then it makes sense that most of the memory is just allocated for the buffer :) Thank you for the hints! I'll investigate it further for myself just to understand the logic behind it |
Description
The following code:
Resulted in this output:
As you can see, escaping from the first
ff()
call or calling theff()
function for the second time did not free the roots of GC even though the GC was supposedly called (the number ofruns
increased) although there were more than 209999 roots in the buffer.Calling manually
gc_collect_cycles
seems to clear the root buffer but it actually does not do anything - callingff()
for the third time actually shows total of 729999 roots in the buffer.Perhaps this is somehow related to the issue mentioned in https://www.php.net/manual/en/features.gc.collecting-cycles.php
The presented case here though has GC enabled all the time, the number of roots seems to be counted and it still leaks memory.
After removing the assignment
$arr[] = $a;
from theff
function, the garbage collector is triggered every 10 000 roots (as documented) and the roots are gone as expected:I got the same output for PHP 7.4, PHP 8.0, and PHP 8.1.
PHP Version
PHP 8.1.7
Operating System
Debian 10
The text was updated successfully, but these errors were encountered: