php.net |  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
Votes:5
Avg. Score:4.4 ± 0.8
Reproduced:5 of 5 (100.0%)
Same Version:2 (40.0%)
Same OS:3 (60.0%)
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
Have you experienced this issue?
Rate the importance of this bug to you:

 [2012-02-06 00:06 UTC] developerguy2008 at yahoo dot com
Description:
------------
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.

Example:

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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-06 07:48 UTC] laruence@php.net
-Type: Bug +Type: Feature/Change Request
 [2013-11-07 10:38 UTC] arjen at react dot com
Slightly related to https://bugs.php.net/bug.php?id=41245 (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:

https://gist.github.com/Danack/69323606f144a2bbb9db
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC