CentOS6.9 (MySql v5.7.22) 使用mysql C API mysql_real_query 导致内存溢出

NOIR 2019-05-23 09:21:22
当我开始循环调用sql语法,比如 UPDATE ** SET ** , mysql_real_query API会导致系统内存持续不断的增长,直到测试进程崩溃或者系统崩溃。我使用top命令查看系统内存的分配情况,很奇怪的是mysqld 和我的测试程序 sqltest都没有看到内存的增加,而系统的free内存一直在减少,used内存一直在增加,最后导致系统崩溃。 代码我实在是检查过很多遍了看不出什么问题, 大神帮我看下。

int ThreadExeSQL(MYSQL* lpSQLConn, char * sql, int iLen)
{

if (mysql_real_query(lpSQLConn, sql, iLen))
{
MYSQL_RES* lpGetSQLRes = mysql_store_result(lpSQLConn);
mysql_free_result(lpGetSQLRes);
return -1;
}

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

MYSQL_RES* lpGetSQLRes = mysql_store_result(lpSQLConn);
mysql_free_result(lpGetSQLRes); // release sql memory

return 0; // success
}

void* ThreadSQL_HexWrite(void* lpGet)
{

LPThreadParam getParam = (LPThreadParam)lpGet;

MYSQL* lpSQLConn = (MYSQL*)&getParam->lpSQLConn;
int iThreadIdx = getParam->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;
}
else
{
printf("mysql_real_connect OK!\n");
}


for (int i = 0; i < 1000000; i++)
{

char lpCmdStr[8192] = "\0";
sprintf(lpCmdStr, "update %s set %s=0x%d where id=%d\0", "tb_Data", "Info", i, 1);

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

continue; //erroe
}
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 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 < thread_num; i++)
{

LPThreadParam lpSetParam = new ThreadParam;
lpSetParam->lpSQLConn = (MYSQL*)&g_MySQLConnList[i];
lpSetParam->iThreadIdx = i;

printf("---create thread idx:%d\n", i);
if (0 != pthread_create(&pTh[i], NULL, ThreadSQL_HexWrite, lpSetParam))
{
printf("pthread_create failed\n");
continue;
}
}

for (int i = 0; i < thread_num; i++)
{
pthread_join(pTh[i], NULL);
}

delete[] pTh;
}

mysql_library_end();

printf("All Done!\n");

}
...全文
66 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧