php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28424 Huge Memory Leak mysql_result()
Submitted: 2004-05-17 21:45 UTC Modified: 2004-05-18 00:47 UTC
From: technik at perlentaucher dot de Assigned:
Status: Not a bug Package: MSSQL related
PHP Version: 5CVS-2004-05-17 (dev) OS: Linux
Private report: No CVE-ID:
 [2004-05-17 21:45 UTC] technik at perlentaucher dot de
Description:
------------
Hi all,

mysql_result seems to leak memory as I Commented 
in Bug #27758. I thought it's worth a new Bugreport.

I tried to find out more about it, so i wrote a 
Script that reads in Data from my DB and calculates
Memory usage in a loop.

The Table looks like that :

28000 identical Records 

ID   : int11, auto_increment
DATA : varchar(20) = 'ABCDEFGHIJKLMNOPQRTS'

The Script reads all Records 300 Times 
before calculating memory usage. 

The Result looks like that : (mem in kb)

Index	Used	Free	Dif
Start	176512	1326208	0
loop 1	176704	1326016	192
loop 2	176776	1325944	72
loop 3	176816	1325904	40
loop 4	176876	1325844	60
loop 5	177028	1325692	152
loop 6	177164	1325556	136
loop 7	177316	1325404	152
loop 8	177492	1325228	176
loop 9	177716	1325004	224
Absolute dif	1204


So usage of Memory is not same in every Loop !!
But at the end I miss about 1.2M of my Memory.

I also could not free this memory by killing 
httpd (killing mysqld also didn't help)

I'm using gentoo, kernel is 2.4.20-gentoo-r7.
Apache : 2.0.48
PHP5   : php5-200405131230 (cvs snap)


Greetings,
 Adam Cwientzek




Reproduce code:
---------------
 for ( $x=0; $x<300; $x++ ) {
    $db = mysql_connect("localhost", "root", "test");
    mysql_select_db("test");
    $sql  = "select data from test"; 
    $result = mysql_query($sql); // causes MEM-Leak !!!!
    mysql_free_result($result);
    mysql_close($db);
 }



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-05-18 00:47 UTC] technik at perlentaucher dot de
MIGHT BE A MYSQL CONFIG ISSUE ?!?!?

While testing i discovered that it seems to stop 
at end of memory without using swap. could be a cache-feature ???

I changed status of this Bug to Bogus until I'm sure that's really a Bug.

greetings,
 Adam
 [2004-10-21 23:23 UTC] jrose at lgb-inc dot com
If this were just a caching problem with MySQL, wouldn't the memory usage show up in MySQL, not inside PHP?

I'm facing the same issue in a database conversion program that has to convert 121,000 records at a time. DB_Result::free() and mysql_free_result() both appear to do nothing at all. Here's my test program (does both PEAR and mysql_ calls) and the results I'm getting with PHP 4.3.2, MySQL 4.0.18, on Redhat Enterprise 3:

<?php
 
define( PEAR, 0 );
 
if ( PEAR )
        require('DB.php');
 
echo "Starting off, we have ", memory_get_usage(), " bytes in use.\n";
 
if ( PEAR )
        $db404 = DB::connect('mysql://cmh:collect@localhost/amis404');
else    {
        $m = mysql_connect( 'localhost', 'user', 'password' );
        mysql_select_db( 'database' );
        }
 
echo "After connecting, we have ", memory_get_usage(), " bytes in use.\n";
 
for( $i = 0; $i < 10; $i++ )
        {
        if ( PEAR )
                $q = $db404->query("select * from really_wide_table limit 30;");
        else    $q = mysql_query("select * from really_wide_table limit 30;");
 
        echo "After query $i, we have ", memory_get_usage(), " bytes in use.\n";
 
        if ( PEAR )
                {
                $q->free();
                unset( $q );
                }
        else    {
                mysql_free_result( $q );
                unset( $q );
                }
 
        echo "After freeing on iteration $i, we have ", memory_get_usage(), " bytes in use.\n";
        }
 
if ( PEAR )
        $db404->disconnect();
else    mysql_close( $m );
 
echo "After disconnecting, we have ", memory_get_usage(), " bytes.\n";
 
?>

And the output, first without PEAR:

bash-2.05b$ php /tmp/test.php
Content-type: text/html
X-Powered-By: PHP/4.3.2
 
Starting off, we have 36024 bytes in use.
After connecting, we have 36760 bytes in use.
After query 0, we have 36896 bytes in use.
After freeing on iteration 0, we have 36928 bytes in use.
After query 1, we have 36952 bytes in use.
After freeing on iteration 1, we have 36992 bytes in use.
After query 2, we have 37016 bytes in use.
After freeing on iteration 2, we have 37056 bytes in use.
After query 3, we have 37080 bytes in use.
After freeing on iteration 3, we have 37120 bytes in use.
After query 4, we have 37144 bytes in use.
After freeing on iteration 4, we have 37184 bytes in use.
After query 5, we have 37208 bytes in use.
After freeing on iteration 5, we have 37248 bytes in use.
After query 6, we have 37272 bytes in use.
After freeing on iteration 6, we have 37312 bytes in use.
After query 7, we have 37336 bytes in use.
After freeing on iteration 7, we have 37376 bytes in use.
After query 8, we have 37400 bytes in use.
After freeing on iteration 8, we have 37440 bytes in use.
After query 9, we have 37464 bytes in use.
After freeing on iteration 9, we have 37504 bytes in use.
After disconnecting, we have 37000 bytes.

Then, with PEAR:

bash-2.05b$ php /tmp/test.php
Content-type: text/html
X-Powered-By: PHP/4.3.2
 
Starting off, we have 227840 bytes in use.
After connecting, we have 497872 bytes in use.
After query 0, we have 499088 bytes in use.
After freeing on iteration 0, we have 499120 bytes in use.
After query 1, we have 499216 bytes in use.
After freeing on iteration 1, we have 499248 bytes in use.
After query 2, we have 499344 bytes in use.
After freeing on iteration 2, we have 499376 bytes in use.
After query 3, we have 499472 bytes in use.
After freeing on iteration 3, we have 499512 bytes in use.
After query 4, we have 499616 bytes in use.
After freeing on iteration 4, we have 499656 bytes in use.
After query 5, we have 499760 bytes in use.
After freeing on iteration 5, we have 499800 bytes in use.
After query 6, we have 499904 bytes in use.
After freeing on iteration 6, we have 499944 bytes in use.
After query 7, we have 500048 bytes in use.
After freeing on iteration 7, we have 500088 bytes in use.
After query 8, we have 500224 bytes in use.
After freeing on iteration 8, we have 500264 bytes in use.
After query 9, we have 500368 bytes in use.
After freeing on iteration 9, we have 500408 bytes in use.
After disconnecting, we have 499904 bytes.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sun Apr 20 01:02:05 2014 UTC