|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-08-24 11:53 UTC] tomas at slax dot org
Description:
------------
If I simply get data from MySQL, PHP consumes more memory for every row:
With some older PHP 5.3 versions, it prints constant number all the time, because RAM is properly freed. But with PHP 5.5.15 and 5.5.16 it prints increased amount of RAM for each row, as like the memory is not properly freed. If the mysql table has lots of rows, PHP script ends on trying to allocate more memory than allowed in php.ini
The same problem appears when using mysql_fetch_row(). The same problem appears when using MySQLi extension instead of mysql. The same problem is there if using PHP on command line, the same problem when using PHP as Apache's module.
I compile PHP from sources, using this configure script:
CFLAGS="-O2 -fPIC" ./configure \
--libdir=/usr/lib64 \
--with-libdir=lib64 \
--disable-debug \
--disable-libxml --disable-simplexml --disable-xml --disable-xmlreader --disable-xmlwriter \
--disable-dom \
--enable-mbstring --enable-mbregex \
--without-sqlite3 \
--without-pdo_sqlite \
--without-pear \
--with-mysql \
--with-mysqli \
--enable-ftp \
--with-iconv \
--with-zlib \
--with-png-dir=/usr \
--with-jpeg-dir=/usr \
--with-openssl \
--with-gd \
--with-freetype-dir=/usr \
--enable-gd-native-ttf \
--with-apxs2=/usr/bin/apxs \
--build=x86_64-slackware-linux
Test script:
---------------
$result=mysql_query("SELECT * FROM table");
while($row=mysql_fetch_assoc($result))
{
echo memory_get_usage()."\n";
}
Expected result:
----------------
I expect PHP to print constant number since it should not allocate more RAM. Like
501320
501320
501320
501320
501320
501320
501320
501320
...
Actual result:
--------------
I get increased number of used RAM
56000376
56001384
56002392
56003400
56004408
56005416
56006424
56007432
56008440
56009448
56010456
56011464
56012472
56013480
56014488
56015496
56016504
56017512
56018520
56019528
56020536
56021544
56022552
56023560
56024568
56025576
56026584
56027592
56028600
56029608
...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 19:00:01 2025 UTC |
Test it yourself, with this simple script: <?php echo "connecting to database...\n"; $link=mysqli_connect("localhost",'root','','test'); echo "creating table with lots of rows...\n"; mysqli_query($link,"CREATE TEMPORARY TABLE tbl123 LIKE mysql.user"); for ($i=0;$i<6000; $i++) { mysqli_query($link,"INSERT IGNORE INTO tbl123 SET host='x".$i."'"); } echo "getting all rows from database\n"; $result=mysqli_query($link,"SELECT * FROM tbl123"); while($row=mysqli_fetch_row($result)) { echo "Mem used: ".number_format(memory_get_usage()/1024,1,'.','')." KB\n"; } // expected result: memory usage still the same // Actual result: memory usage increases with each row :( ?>