|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-09-08 19:04 UTC] romain dot riviere at gmail dot com
Description:
------------
While trying to diagnose apparently random Out of Memory errors in various PHP scripts (including Drupal among others), I ended up using the code pasted below for testing purposes.
Browing the resulting page several times always results in an ever-increasing VSZ for apache processes (up to 4GB in my case) and eventually Out of Memory errors way before the script itself actually reaches the limit. The script was called with size=61 in my case (memory_limit=64M). On an 8GB system, it might take as many as 30 requests to recreate the bug. With a higher memory limit and value of the "size" parameter, it will probably happen much sooner.
PHP compiled with
Reproduce code:
---------------
<?php
ini_set('display_errors',true);
if (isset($_GET['size']) && $_GET['size'] < 200) {
$mb = intval($_GET['size']);
}
else {
$mb=5;
}
$var = '';
echo 'Memory limit: '.ini_get('memory_limit').'<br>';
for ($i=0; $i<=$mb; $i++) {
$var.= str_repeat('a',1*1024*1024);
echo memory_get_usage().'<br>';
}
echo 'String length: '. strlen($var);
?>
Expected result:
----------------
For the first few runs, the script correctly displays the memory usage and the value is consistent with the memory limit :
Memory limit: 64M
<snip>
String length: 65011712
Actual result:
--------------
Memory limit: 64M
<snip>
Fatal error: Out of memory (allocated 61341696) at /var/tmp/portage/dev-lang/php-5.2.10/work/php-5.2.10/ext/standard/string.c:4599 (tried to allocate 1048577 bytes) in /home/acrm/OF-svn/2.0/test.php on line 14
Subsequent runs :
Fatal error: Out of memory (allocated 262144) at /var/tmp/portage/dev-lang/php-5.2.10/work/php-5.2.10/ext/standard/string.c:4599 (tried to allocate 1048577 bytes) in /home/acrm/OF-svn/2.0/test.php on line 14
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 06:00:01 2025 UTC |
The problem still happens without --enable-debug. I could reproduce the bug in CLI with a simple while(true). More specifically (and with memory_limit=128M this time): <?php ini_set('display_errors',true); if (isset($_GET['size']) && $_GET['size'] < 200) { $mb = intval($_GET['size']); } else { $mb=125; } while(true) { $var = ''; echo 'Memory limit: '.ini_get('memory_limit').'<br>'; for ($i=0; $i<=$mb; $i++) { $var.= str_repeat('a',1*1024*1024); echo memory_get_usage().'<br>'; } echo 'String length: '. strlen($var); } ?> This script crashes after a few iterations with the message: Fatal error: Out of memory (allocated 123469824) (tried to allocate 122683393 bytes) in /home/acrm/OF-svn/2.0/test.php on line 14 zend_mm_heap corrupted The memory_get_usage() output increases until it busts the 128M limit. Now, I'm not too familiar with PHP's memory management, but I find it strange that the script should hog memory that way: shouldn't it free some RAM when $var is reset to '' ? At any rate, the initial issue remains, whether this one is related or not.