|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60982 GC not called prior to memory exhausted error
Submitted: 2012-02-06 00:06 UTC Modified: 2012-02-06 07:48 UTC
Avg. Score:4.5 ± 0.8
Reproduced:13 of 13 (100.0%)
Same Version:3 (23.1%)
Same OS:10 (76.9%)
From: developerguy2008 at yahoo dot com Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 5.4.0RC7 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: developerguy2008 at yahoo dot com
New email:
PHP Version: OS:


 [2012-02-06 00:06 UTC] developerguy2008 at yahoo dot com
This affects all versions of PHP. In a script that is about to run out of memory, gc_collect_cycles() should be called as this can sometimes prevent the script from running out of memory.


I have a non-blocking server I wrote in PHP. If there are several thousand connections, the script will run out of memory. However, if the exact same script with the exact same load (using apache benchmark) is run while calling gc_collect_cycles() every half second, there are no out-of-memory issues.

Test script:
The source code for PHP will reveal that garbage collection is not run as a last ditch effort to continue execution within the memory constraints specified by the user.

Expected result:
I expect PHP to run garbage collection when memory is nearly exhausted to prevent a memory error.

Actual result:
PHP will exhaust memory even when it is not necessary for it to exhaust memory.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-06 07:48 UTC]
-Type: Bug +Type: Feature/Change Request
 [2013-11-07 10:38 UTC] arjen at react dot com
Slightly related to (Ability to set handler for "memory limit exceeded")
 [2014-11-04 16:09 UTC] Danack at basereality dot com
PHP does not call the function gc_collect_cylces automatically. Although it can be called manually by users, it seems odd that PHP doesn't automatically call the GC either:

* when PHP detects that it is going to be able to unable to allocate more memory.

* automatically when memory usage reaches a critical but not fatal limit e.g. 90% memory used.

The linked script below uses 786kB when gc_collect_cycles is called every loop. When the gc_collect_cycles is not called, it exceeds the memory limit of 64MB.

The cause of this issue is that the allocations are large and few. Because of this the number of 'root buffers' the Garbage Collection is aware of never reaches 10,000 and so it never calls itself automatically.

From the GC benchmark output, the values are:
    Root buffer length: 254
    Root buffer peak:   260

i.e. The script would need forty times more root buffer entries before the GC would kick in automatically, which would probably require changing the memory limit from 64MB to 2.5 gigabytes, for a script that actually only uses 700KB of memory.

Example is too long for here, please look at:
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 20 14:01:34 2024 UTC