Bug #16017 Memory leak in mysql embedded library
Submitted: 27 Dec 2005 13:50 Modified: 2 Aug 2006 17:52
Reporter: Noam Ambar Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Embedded Library ( libmysqld ) Severity:S3 (Non-critical)
Version:4.1.13, 5.1 OS:Linux (Linux (SuSE 9 kernel 2.6))
Assigned to: Alexey Botchkov CPU Architecture:Any

[27 Dec 2005 13:50] Noam Ambar
Description:
When I run the program below (mysql_embedded), it causes a memory leak of ~6MB. When I add more iterations to the loop, it consumes the entire machine's memory. valgrind shows that the leak is on mysql_server_init but I'm not sure that this is not true since the leak is increased when I add iterations to the loop. Table T1 is an empty table with one column (int i1).

I ran the same program linked with the client library and changed the connection to a local mysql server and it worked fine. There were not leaks on both server and client.

Also when I replace the "SELECT" statement with "UPDATE" statement there is no memory leak so I guess that it is related to the result set but I don't know how. 

I get the same results on MySql4 and MySql5. 

Any response will be appreciated. 

Thanks, 

Noam 

valgrind output: 

==9484== 6004992 bytes in 2911 blocks are definitely lost in loss record 14 of 16 
==9484== at 0x1B904595: malloc (vg_replace_malloc.c:130) 
==9484== by 0x806757A: my_malloc (my_malloc.c:35) 
==9484== by 0x8061669: init_dynamic_array (in /a/nfs/iil/iec/sws/work/nambar/development/head/common/cpp/mysql/testfeeder) 
==9484== by 0x8165A61: _hash_init (in /a/nfs/iil/iec/sws/work/nambar/development/head/common/cpp/mysql/testfeeder) 
==9484== by 0x80AFF94: init_max_user_conn() (sql_parse.cc:503) 
==9484== by 0x8073AF2: init_embedded_server (mysqld.cc:3115) 
==9484== by 0x8056813: mysql_server_init (libmysql.c:162) 
==9484== by 0x804D6E8: CMySqlDatabase::startup(std::string&) (CMySqlDatabase.cpp:76) 
==9484== by 0x805241E: main (main.cpp:13)

How to repeat:
int main() 
{ 

string path = "/tmp/testarea"; 
CMySqlDatabase::startup(path); 
mysql_thread_init(); 

MYSQL* handle = mysql_init(NULL); 
mysql_real_connect(handle, NULL, NULL, NULL, "netstar", 0, NULL, 0); 
for(int i=0; i<3000; i++) 
{ 
cout << "iteration " << i << endl; 
MYSQL_STMT* stmt = mysql_stmt_init(handle); 
char* command = "SELECT 1 FROM T1"; 
//char* command = "UPDATE T1 SET i1 = 1"; 
mysql_stmt_prepare(stmt, command, strlen(command)); 
mysql_stmt_execute(stmt); 
mysql_stmt_free_result(stmt); 
mysql_stmt_close(stmt); 
} 
mysql_close(handle); 

} 
}
[2 Jan 2006 13:46] Hartmut Holzgraefe
I wasn't able to test with latest 5.0 source, but with 4.1 i see the process growing over time (a megabyte every few seconds) when using the following source:

#include <stdlib.h>
#include <stdio.h>

#include <mysql.h>

static char *server_groups[] = {
  "embedded",
  "server",
  (char *)NULL
};

int main(int argc, char **argv) 
{
  MYSQL *mysql = NULL;
  int i;
  char query[256];

  if (mysql_server_init(argc, argv, server_groups)) {
	fputs("server init failed", stderr);
	return EXIT_FAILURE;
  }

  mysql_thread_init(); 
  
  MYSQL* handle = mysql_init(NULL); 
   mysql_options(handle, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");
   mysql_options(handle, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);  mysql_real_connect(handle, NULL, NULL, NULL, "test", 0, NULL, 0); 
  for(i=0; 1; i++) 
  { 
	printf("iteration %d\n", i); 
	MYSQL_STMT* stmt = mysql_stmt_init(handle); 
	char* command = "SELECT 1 FROM i1"; 
//char* command = "UPDATE T1 SET i1 = 1"; 
	mysql_stmt_prepare(stmt, command, strlen(command)); 
	mysql_stmt_execute(stmt); 
 	mysql_stmt_store_result(stmt);
	while (!mysql_stmt_fetch(stmt)) {}
	mysql_stmt_free_result(stmt); 
	mysql_stmt_close(stmt); 
  } 
  mysql_close(handle); 
  
}
[2 Jan 2006 14:01] Noam Ambar
Thank you for verifying it.
[5 Jan 2006 8:19] Noam Ambar
Changing category to libmysqld
[2 Feb 2006 9:47] Noam Ambar
After making some more tests I found out that this problem is related to the structure of the database. This problem exists in mysql5 when using database that was originally created with mysql4. If the database was originally created with mysql5, the memory leak does not show up
[1 Jun 2006 11:49] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/7155
[14 Jul 2006 16:15] Paul DuBois
Noted in 4.1.21 changelog.

Some memory leaks in the libmysqld embedded server were corrected.

Returning report to NDI status pending push of fix into 5.1.
[2 Aug 2006 17:52] Paul DuBois
Noted in 5.1.12 changelog.