Req #60982 [Com]: GC not called prior to memory exhausted error

From: Date: Tue, 04 Nov 2014 16:09:48 +0000
Subject: Req #60982 [Com]: GC not called prior to memory exhausted error
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-188443@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=60982&edit=1

 ID:                 60982
 Comment by:         Danack at basereality dot com
 Reported by:        developerguy2008 at yahoo dot com
 Summary:            GC not called prior to memory exhausted error
 Status:             Open
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 Operating System:   Linux
 PHP Version:        5.4.0RC7
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2013-11-07 10:38:41] arjen at react dot com

Slightly related to https://bugs.php.net/bug.php?id=41245 (Ability to
set handler for "memory limit exceeded")

------------------------------------------------------------------------
[2012-02-06 00:06:51] 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.


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=60982&edit=1


Thread (3 messages)

« previous php.bugs (#188443) next »