You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Memory allocated by stream_context_create not released when destroying stream_socket_server #10885
I have a long running PHP application that is creating stream_socket_server over time, the memory allocated by stream_context_create() is never released even after closing and destroying the stream_socket_server. A minimal reproduction is below (backlog is set to allocate more memory on each loop however even an empty array to stream_context_create shows this issue). Eventually the application will crash, this is more evident in memory constrained environments like container deployments.
I found an old PHP bugs report referencing this with stream_socket_client (https://bugs.php.net/bug.php?id=61371) inhibition for deleting the context was too broad and a really old one that says that stream_context_create() memory is never released https://bugs.php.net/bug.php?id=40257 but this is not documented.
Output from memprof shows the allocated memory from stream_context_create()
changed the title [-]Memory allocated by stream_context_create not released[/-][+]Memory allocated by stream_context_create not released when destroying stream_socket_server[/+]on Mar 20, 2023
Meaning you've seen this issue actually crash the application? Or are you saying that it would hypothetically if it continued?
Because sometimes PHP will hold onto memory longer than it needs to, and it may appear that memory is leaking when actually PHP is simply not letting go of it yet.
@damianwadley It crashes the application with PHP Fatal error: Allowed memory size.
You can see in the example in the original report that if you stick a ini_set('memory_limit','40M'); at the top, the script will crash after 40214 iterations on PHP8.0.28 (faster on 8.2 as it uses more memory per context).
@iluuu1994 , I was going through this issue and was curious to know if this is applicable to :PHP 7.
I understand PHP 7 is out of support. But I do also observed seen in memory usage.
Thanks !.
@HeenaBansal2009 , yes https://3v4l.org/XBaaP shows that it was observable all the way back to PHP5.2 and likely before (my testcase uses memory_get_usage() which didn't exists prior to that)
Activity
[-]Memory allocated by stream_context_create not released[/-][+]Memory allocated by stream_context_create not released when destroying stream_socket_server[/+]damianwadley commentedon Mar 20, 2023
Meaning you've seen this issue actually crash the application? Or are you saying that it would hypothetically if it continued?
Because sometimes PHP will hold onto memory longer than it needs to, and it may appear that memory is leaking when actually PHP is simply not letting go of it yet.
lucasnetau commentedon Mar 20, 2023
@damianwadley It crashes the application with PHP Fatal error: Allowed memory size.
You can see in the example in the original report that if you stick a
ini_set('memory_limit','40M');at the top, the script will crash after 40214 iterations on PHP8.0.28 (faster on 8.2 as it uses more memory per context).Merge branch 'PHP-8.2'
HeenaBansal2009 commentedon Jul 3, 2023
@iluuu1994 , I was going through this issue and was curious to know if this is applicable to :PHP 7.
I understand PHP 7 is out of support. But I do also observed seen in memory usage.
Thanks !.
lucasnetau commentedon Jul 3, 2023
@HeenaBansal2009 , yes https://3v4l.org/XBaaP shows that it was observable all the way back to PHP5.2 and likely before (my testcase uses memory_get_usage() which didn't exists prior to that)