From:             clyde dot adams at mailismagic dot com
Operating system: OSX Server 10.5.8
PHP version:      Irrelevant
Package:          *General Issues
Bug Type:         Bug
Bug description:Random memory leak between runs with identical data inputs

Description:
------------
While we are running PHP 5.3.27 here, I see nothing in the changelog for
newer versions of PHP that indicates this bug is even possibly fixed.  The
code on pastebin is a simplification of the scenario.  In my real-world
environment, a query on a database is run, results are built up in an array
in PHP, and then boiled down into a second array.  Everything is fine until
the inner foreach() loop.  At that point, all bets are off as to whether or
not PHP will run itself out of memory.  The exact same inputs resulting in
the exact same data in $rows2 will sometimes cause PHP to suddenly chew up
128MB RAM and terminate the script - interestingly, not passing 'true' to
memory_get_usage() says only 40MB is being used at the time PHP terminates
the script while claiming 128MB RAM is being used.  The problem seems to
hinge on this line:

$row[1] = htmlspecialchars(implode(", ", $idmap[$row[1]]));

If I break it out so the variable being assigned doesn't reference itself
in the same statement:

$id = $idmap[$row[1]];
$row[1] = htmlspecialchars(implode(", ", $id));

The problem goes away as well.  There is nothing involved in the foreach
statements beyond basic PHP arrays, numbers, and strings (i.e. no objects).
 The amount of memory used before the foreach() lines is normal (about
10MB).  When the foreach statements complete successfully, the amount of
memory used is still normal (about 20MB).  So there isn't always a runaway
memory leak.

Attempting to replicate this issue will likely cause headaches.  When I
added the commented out if-statement so I could output debugging
information without affecting other users, the problem went away...even
though the if-statement technically does absolutely nothing.  This made
pinpointing the problem a lot more difficult in userland PHP.

There is indeed a bug here.  I wouldn't be filing a bug report or writing a
test script if I wasn't certain about the issue.

Test script:
---------------
http://pastebin.com/z1KBmJLN

Expected result:
----------------
Consistent memory usage between runs with identical data.  If statements
that do nothing should not have an effect on memory usage.

Actual result:
--------------
PHP inexplicably consumes all memory up to the limit in the INI file.  But
only sometimes.  For the exact same inputs and data.

-- 
Edit bug report at https://bugs.php.net/bug.php?id=65775&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=65775&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=65775&r=trysnapshot53
Try a snapshot (trunk):     
https://bugs.php.net/fix.php?id=65775&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=65775&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=65775&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=65775&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=65775&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=65775&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=65775&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=65775&r=notwrong
Not enough info:            
https://bugs.php.net/fix.php?id=65775&r=notenoughinfo
Submitted twice:            
https://bugs.php.net/fix.php?id=65775&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=65775&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65775&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=65775&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=65775&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=65775&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=65775&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=65775&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=65775&r=mysqlcfg

Reply via email to