List:General Discussion« Previous MessageNext Message »
From:Michael Widenius Date:April 15 1999 11:34am
Subject:Memory leaking like a sieve in mysql client api.
View as plain text  
>>>>> "Benjamin" == Benjamin Scherrey <scherrey@stripped> writes:

Benjamin> I'm getting some unusual behavior out of the mysql client apis. Try the
Benjamin> following:

Benjamin> // test1 results in your code never returning.
Benjamin> void test1( void )
Benjamin> {
Benjamin>     MYSQL* mysql = mysql_init( NULL );

Benjamin>     mysql.close( mysql );
Benjamin> };

This doesn't return because the API requires that you call mysql_real_connect()
or mysql_connect() after mysql_init().

Here is a fix if you really want to do the above:

*** /my/monty/master/mysql-3.22.21/client/libmysql.c    Mon Feb 22 14:10:54 1999
--- ./libmysql.c        Thu Apr 15 14:15:20 1999
****************
*** 902,907 ****
--- 902,908 ----
      if (!(mysql=(MYSQL*) my_malloc(sizeof(*mysql),MYF(MY_WME | MY_ZEROFILL))))
        return 0;
      mysql->free_me=1;
+     mysql->net.fd= INVALID_SOCKET;
    }
    else
      bzero((char*) (mysql),sizeof(*(mysql)));


Benjamin> // test2 - Keeps growing memory and will not release until process ends.
Benjamin> Leak?
Benjamin> // Eventually as 'i' grows, a connection error will occur and the
Benjamin> routine will error out.
Benjamin> void test2( const char* db, const char* host, const char* user, const
Benjamin> char* pwd )
Benjamin> {
Benjamin>     for( int i = 0;i<3000;i++ )
Benjamin>     {
Benjamin>         MSQL* mysql = mysql_init( NULL );

Benjamin>         if( !mysql_real_connect( mysql, host, user, pwd, 0, NULL, 0 ) )
Benjamin>         {
Benjamin>             cout << mysql_error( mysql ) << endl;
Benjamin>             exit(1);
Benjamin>         }

Benjamin>         mysql_close( mysql );
Benjamin>     }
Benjamin> };

Benjamin>     How do I get mysql to fully release its memory without killing the
Benjamin> process? Why does test1 never return? This is getting a tad bit
Benjamin> frustrating trying to track what appears to be some undefinable behavior
Benjamin> in the c api. I'd appreciate any help with this.

Hi!

Please include a full tested program with all error messages and post
your questions using the mysqlbug script)!
(Your original program didn't work, and the error is very likely an OS 
bug).

I just tested MySQL 3.22.21 with the following program:

-------
#include <stdio.h>
#include <mysql.h>

void test2( const char* db, const char* host, const char* user, const
char* pwd )
{
  int i;
    for( i = 0;i<10000;i++ )
    {
        MYSQL* mysql = mysql_init( 0 );

        if( !mysql_real_connect( mysql, host, user, pwd, 0, 0, 0,0 ) )
        {
	  printf("%s",mysql_error( mysql ));
	  exit(1);
        }
	printf("%8d",i);
        mysql_close( mysql );
    }
};

int main()
{
  test2("test","localhost",0,0);
  exit(0);
}
--------

This runs without any problems and without any memory loss!

Note that if you change 'localhost' to a hostname, the above will die
after a while with Linux 2.2 with something like:

Can't connect to MySQL server on 'monty.pp.sci.fi' (11)
shell> perror 11
Resource temporarily unavailable

The above is a bug in the 2.2  kernel;  If you make many fast TCP/IP
connections (in my case more than 3972), Linux will refuse new
connections for a while (a couple of seconds).
I have verified the above with all kernels 2.2.0 -> 2.2.5.
The above doesn't happen with the 2.0.36 kernel!

Regards,
Monty



Thread
Memory leaking like a sieve in mysql client api.Benjamin Scherrey15 Apr
  • Re: Memory leaking like a sieve in mysql client api.Benjamin Scherrey15 Apr
  • Re: Memory leaking like a sieve in mysql client api.Benjamin Scherrey15 Apr
  • Re: Memory leaking like a sieve in mysql client api.Benjamin Scherrey15 Apr
  • Re: Memory leaking like a sieve in mysql client api.Benjamin Scherrey15 Apr
    • Re: Memory leaking like a sieve in mysql client api.Michael Widenius15 Apr
  • Memory leaking like a sieve in mysql client api.Michael Widenius15 Apr
  • Re: Memory leaking like a sieve in mysql client api.Benjamin Scherrey15 Apr
    • Re: Memory leaking like a sieve in mysql client api.Michael Widenius16 Apr
      • Re: Memory leaking like a sieve in mysql client api..16 Apr
        • Re: Memory leaking like a sieve in mysql client api.Michael Widenius17 Apr
Re: Memory leaking like a sieve in mysql client api.Fred Lindberg15 Apr
Re: Memory leaking like a sieve in mysql client api.Fred Lindberg15 Apr