From:
Operating system: FreeBSD
PHP version: 5.3.6
Package: MySQLi related
Bug Type: Bug
Bug description:Memory leak associated with mysql connector
Description:
------------
I have found a memory leak in connection with several production PHP
scripts.
While there are many bug reports relating to memory leaks, I did not find
anything similar to our situation, which is very easy to reproduce.
The scripts that have been affected by this memory leak have been in
continuous
production use since 2006. We did not notice a memory leak prior to when we
first upgraded to PHP 5.3.5. It is possible that there was a smaller leak
prior
to this time that merely escaped notice. However, before reporting the
problem,
we upgraded to 5.3.6 to make sure it had not been corrected. The results in
this
ticket are thus for 5.3.6.
Below I have given the main loop of a "small reproducible code." As you can
see,
the only thing done in this test is to fetch the rows, and print out memory
usage every 3000 rows.
Regarding the mysql connector: originally the test was run with
mysqli_connect.
It was suggested via Experts-Exchange to try the mysql_connector. This was
done
but the results were identical. The full script can works with both
mysql_connect and mysqli_connect, controlled by a define.
The bug signs:
1. On 5.3.6, the "after SQL" memory usage jumps to 13MB, whereas on 5.2.4
it
stays at the initial low value (262144 on 5.2.4 but 786432 on 5.3.6).
2. On 5.3.6, the memory usage grows dramatically, whereas on 5.2.4 it does
not
(the "id" lines are displayed after each 3000 rows, where the id is the
primary
key for the row). In the production scripts, this leads to a crash when the
memory limit is exceeded.
Please note:
1. the SQL statement is composed of a UNION of 5 relatively simple SELECTs,
making the single statement relatively complex.
2. The expected and actual results shown below are achieved by connecting
to the
same database.
I you would like the URL of the actual script, which has the connection
routines
and the SQL (which is relatively complex), please let me know as I would
have to
remove the passwords, etc. I could also send a mysqldump of a sanitized
version
of the database. The bzip2 of the dump file is about 35MB.
Test script:
---------------
echo "Test Autoemail Memory Leak\n";
echo 'start run mem=' . memory_get_usage(true) . "\n";
$query = get_query();
$rs = SQL($link, $query);
if ($rs)
{
// main loop
$cnt = 0;
echo 'after SQL mem=' . memory_get_usage(true) . "\n";
$func = (MYSQL_ENHANCED) ? 'mysqli_fetch_row' : 'mysql_fetch_row';
while($row = $func($rs))
{
if (++$cnt % 3000 == 0)
{
echo ' id=' . $row[1] . ' mem=' .
memory_get_usage(true) . "\n";
}
}
echo "EOJ\n";
}
else
{
echo $errmsg;
}
Full script is at http://www.rephunter.net/test-autoemail-memory.php. The
web server will execute the script if accessed in a browser.
Expected result:
----------------
Showing no leak on Windows VM PHP 5.2.4 connecting to same database
F:\Websites\RepHunter\current>php -v
PHP 5.2.4 (cli) (built: Oct 1 2007 20:06:42)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
with Zend Core v2.5.0, Copyright (c) 1998-2006, by Zend Technologies
with Zend Extension Manager v1.2.0, Copyright (c) 2003-2006, by Zend
Technol
ogies
with Zend Optimizer v3.3.1, Copyright (c) 1998-2007, by Zend
Technologies
with Zend Debugger v5.2.10, Copyright (c) 1999-2007, by Zend
Technologies
F:\Websites\RepHunter\current>php test-autoemail-memory.php
Test Autoemail Memory Leak
Using mysqli_connect
start run mem=262144
after SQL mem=262144
id=43655 mem=262144
id=40250 mem=262144
id=37355 mem=262144
id=34419 mem=262144
id=31544 mem=262144
id=28915 mem=262144
id=26168 mem=262144
id=21461 mem=262144
id=16550 mem=262144
id=13074 mem=262144
id=9140 mem=262144
id=3892 mem=262144
EOJ
F:\Websites\RepHunter\current>
Actual result:
--------------
Showing the leak on 5.3.6
[jas1@www /var/www/rephunter/www/webroot]$ php -v
PHP 5.3.6 with Suhosin-Patch (cli) (built: May 13 2011 21:58:30)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
[jas1@www /var/www/rephunter/www/webroot]$ php ./test-autoemail-memory.php
Test Autoemail Memory Leak
Using mysqli_connect
start run mem=786432
after SQL mem=13631488
id=43655 mem=23592960
id=40250 mem=33292288
id=37355 mem=43253760
id=34419 mem=52953088
id=31544 mem=62914560
id=28915 mem=72613888
id=26168 mem=82575360
id=21461 mem=92274688
id=16550 mem=102236160
id=13074 mem=112197632
id=9140 mem=121896960
id=3892 mem=131858432
EOJ
[jas1@www /var/www/rephunter/www/webroot]$
--
Edit bug report at http://bugs.php.net/bug.php?id=54864&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=54864&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=54864&r=trysnapshot53
Try a snapshot (trunk):
http://bugs.php.net/fix.php?id=54864&r=trysnapshottrunk
Fixed in SVN:
http://bugs.php.net/fix.php?id=54864&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=54864&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=54864&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=54864&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=54864&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=54864&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=54864&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=54864&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=54864&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=54864&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=54864&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=54864&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=54864&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=54864&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=54864&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=54864&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=54864&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=54864&r=mysqlcfg