Bug #73491 [NEW]: Memory leak in simple loop through database results

From: Date: Thu, 10 Nov 2016 21:49:02 +0000
Subject: Bug #73491 [NEW]: Memory leak in simple loop through database results
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-205291@lists.php.net to get a copy of this message
From: michael at logaholic dot com Operating system: Ubuntu 16.04 PHP version: 5.6.28 Package: MySQLi related Bug Type: Bug Bug description:Memory leak in simple loop through database results Description: ------------ While reading results from the database, memory usage increases on each iteration until the script runs out of memory. I have tested this on two machines with the same result: Dev server: PHP 5.6.24 on Ubuntu 16.04, with MariaDB 10.1.16 as provided by Xampp. Production server: PHP 5.6.27 on CentOS with Mysql. Calling mysqli_fetch_array() seems to cause the problem (any loop without it does not cause memory usage to keep growing) The same script with older versions of PHP worked fine. This is my configure line: './configure' '--prefix=/opt/lampp' '--with-apxs2=/opt/lampp/bin/apxs' '--with-config-file-path=/opt/lampp/etc' '--with-mysql=mysqlnd' '--enable-inline-optimization' '--disable-debug' '--enable-bcmath' '--enable-calendar' '--enable-ctype' '--enable-ftp' '--enable-gd-native-ttf' '--enable-magic-quotes' '--enable-shmop' '--disable-sigchild' '--enable-sysvsem' '--enable-sysvshm' '--enable-wddx' '--with-gdbm=/opt/lampp' '--with-jpeg-dir=/opt/lampp' '--with-png-dir=/opt/lampp' '--with-freetype-dir=/opt/lampp' '--with-zlib=yes' '--with-zlib-dir=/opt/lampp' '--with-openssl=/opt/lampp' '--with-xsl=/opt/lampp' '--with-ldap=/opt/lampp' '--with-gd' '--with-imap=/bitnami/xamppunixinstallerstackDev-linux-x64/src/imap-2007e' '--with-imap-ssl' '--with-gettext=/opt/lampp' '--with-mssql=shared,/opt/lampp' '--with-pdo-dblib=shared,/opt/lampp' '--with-sybase-ct=/opt/lampp' '--with-mysql-sock=/opt/lampp/var/mysql/mysql.sock' '--with-oci8=shared,instantclient,/opt/lampp/lib/instantclient' '--with-mcrypt=/opt/lampp' '--with-mhash=/opt/lampp' '--enable-sockets' '--enable-mbstring=all' '--with-curl=/opt/lampp' '--enable-mbregex' '--enable-zend-multibyte' '--enable-exif' '--with-bz2=/opt/lampp' '--with-sqlite=shared,/opt/lampp' '--with-sqlite3=/opt/lampp' '--with-libxml-dir=/opt/lampp' '--enable-soap' '--with-xmlrpc' '--enable-pcntl' '--with-mysqli=mysqlnd' '--with-pgsql=shared,/opt/lampp/' '--with-iconv=/opt/lampp' '--with-pdo-mysql=mysqlnd' '--with-pdo-pgsql=/opt/lampp/postgresql' '--with-pdo_sqlite=/opt/lampp' '--with-icu-dir=/opt/lampp' '--enable-fileinfo' '--enable-phar' '--enable-zip' '--enable-intl' 'CC=gcc' 'CFLAGS=-I/opt/lampp/include/c-client '-I/opt/lampp/include/libpng' '-I/opt/lampp/include/freetype2' '-O3' '-fPIC' '-L/opt/lampp/lib' '-I/opt/lampp/include' '-I/opt/lampp/include/ncurses'' 'LDFLAGS=-Wl,--rpath '-Wl,/opt/lampp/lib' '-L/opt/lampp/lib' '-I/opt/lampp/include' '-L/opt/lampp/lib' '-L/opt/lampp'' 'CPPFLAGS=-I/opt/lampp/include/c-client '-I/opt/lampp/include/libpng' '-I/opt/lampp/include/freetype2' '-O3' '-fPIC' '-L/opt/lampp/lib' '-I/opt/lampp/include' '-I/opt/lampp/include/ncurses'' 'CXX=g++' 'CXXFLAGS=-I/opt/lampp/include/c-client '-I/opt/lampp/include/libpng' '-I/opt/lampp/include/freetype2' '-I/opt/lampp/include/ncurses' '-O3' '-L/opt/lampp/lib' '-I/opt/lampp/include'' Test script: --------------- $sql = "select field from mytable limit 5000"; $data = mysqli_query($db->_connectionID, $sql); $n=0; while ($row = mysqli_fetch_array($data)) { $n++; echo "$n ) "; echo number_format((memory_get_usage() / 1024)/1024, 2); echo " mb<br>"; } //Note I am using 3 echo statements as I read somewhere that concatenating can cause memory problems Expected result: ---------------- I expect the memory usage to stay pretty constant while the script runs. Actual result: -------------- The actual output of the test script looks like this: 1 ) 7.22 mb 2 ) 7.22 mb 3 ) 7.22 mb 4 ) 7.23 mb ... ... 4998 ) 14.08 mb 4999 ) 14.09 mb 5000 ) 14.09 mb Memory usage just keeps growing steadily with each line. -- Edit bug report at https://bugs.php.net/bug.php?id=73491&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=73491&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=73491&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=73491&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=73491&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=73491&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=73491&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=73491&r=needscript Try newer version: https://bugs.php.net/fix.php?id=73491&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=73491&r=support Expected behavior: https://bugs.php.net/fix.php?id=73491&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=73491&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=73491&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=73491&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=73491&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=73491&r=dst IIS Stability: https://bugs.php.net/fix.php?id=73491&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=73491&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=73491&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=73491&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=73491&r=mysqlcfg

« previous php.bugs (#205291) next »