Home > database >  CentOS6.9 (MySql v5.7.22) using MySql C API mysql_real_query lead to memory overflow
CentOS6.9 (MySql v5.7.22) using MySql C API mysql_real_query lead to memory overflow

Time:09-21

When I started cycle call SQL syntax, such as UPDATE SET * *, * * mysql_real_query API continuous growth can lead to system memory, until the crash test process or a system crash, I use the top command to check the system memory allocations, oddly mysqld sqltest test program and I did not see the increase of the memory, the system of free memory has been declining, informs the memory has been on the rise, the final result in system crash, the code is checked many times I see what's the problem, not a great god help me to look at the

Int ThreadExeSQL (SQL, MYSQL * lpSQLConn, char * int iLen)
{

If (mysql_real_query (lpSQLConn, SQL, iLen))
{
MYSQL_RES * lpGetSQLRes=mysql_store_result (lpSQLConn);
The mysql_free_result (lpGetSQLRes);
return -1;
}

//mysql_errno (lpSQLConn);
//mysql_error (lpSQLConn);

MYSQL_RES * lpGetSQLRes=mysql_store_result (lpSQLConn);
The mysql_free_result (lpGetSQLRes);//release the memory SQL

return 0;//success
}

Void * ThreadSQL_HexWrite (void * lpGet)
{

LPThreadParam getParam=(LPThreadParam lpGet);

MYSQL * lpSQLConn=(MYSQL *) & amp; GetParam - & gt; LpSQLConn;
Int iThreadIdx=getParam - & gt; IThreadIdx;

Printf (" ID: % d \ n ", iThreadIdx);

Mysql_thread_init ();

LpSQLConn=mysql_init (NULL);


if (! Mysql_real_connect (lpSQLConn g_host_name, g_user_name g_password, g_db_name, g_db_port, NULL, 0))
{
ThreadSQLError (lpSQLConn, NULL);
return;
}
The else
{
Printf (" mysql_real_connect OK! \n");
}


for (int i=0; I & lt; 1000000; I++)
{

Char lpCmdStr [8192]="\ 0";
Sprintf (lpCmdStr, "set the update % s % s=0 x % d where id=% d \ 0" and "tb_Data", "Info", I, 1);

If (ThreadExeSQL (lpSQLConn (char *) lpCmdStr, strlen (lpCmdStr)))
{
MySQLError getError=ThreadSQLError (lpSQLConn, NULL);
HandleMySqlError (getError);

continue;//erroe
}
The else
{
Printf (" ok ");
}

Usleep (1000 * 10);
}

Mysql_close (lpSQLConn);

Mysql_thread_end ();


Printf (" ThreadSQL_HexWrite OK! \n");
}


MYSQL * g_MySQLConnList [100];

Void main ()
{

If (mysql_library_init (0, NULL, NULL))
{
Printf (" could not initialize the MySQL client library \ n ");
exit(1);
}


Int thread_num=1;

//while (true)
{
Pthread_t * pTh=new pthread_t [thread_num];


for (int i=0; I & lt; Thread_num; I++)
{

LPThreadParam lpSetParam=new ThreadParam;
LpSetParam - & gt; LpSQLConn=(MYSQL *) & amp; G_MySQLConnList [I];
LpSetParam - & gt; IThreadIdx=I;

Printf (" -- -- -- the create thread independence idx: % d \ n ", I);
if (0 !=pthread_create (& amp; PTh [I], NULL, ThreadSQL_HexWrite lpSetParam))
{
Printf (" pthread_create failed \ n ");
continue;
}
}

for (int i=0; I & lt; Thread_num; I++)
{
Pthread_join (pTh [I], NULL);
}

The delete [] pTh;
}

Mysql_library_end ();

Printf (" All Done! \n");

}
  • Related