By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,899 Members | 1,230 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,899 IT Pros & Developers. It's quick & easy.

CentOS6.9 (MySql v5.7.22) use mysql C API mysql_real_query cause the memory always

P: 2
I test the mysql_real_query API, I just loop to execute Sql syntax ,like 'UPDATE ** SET **' there is a leak memory bug occur. when I use 'top' to check the bug, I find the system 'used memory' option will always growing until the system or process crush. but 'mysqld' and 'testsql' processes's %MEM option has not increase, System free memory look like disappear. I try to force kill the 'testsql' process but the memory still be used and can not be release. Why? Please help me.

Expand|Select|Wrap|Line Numbers
  1. int ThreadExeSQL(MYSQL* lpSQLConn, char * sql, int iLen)
  2. {
  3.  
  4.     if (mysql_real_query(lpSQLConn, sql, iLen))
  5.     {
  6.         MYSQL_RES* lpGetSQLRes = mysql_store_result(lpSQLConn);
  7.         mysql_free_result(lpGetSQLRes);
  8.         return -1;
  9.     }
  10.  
  11.     //mysql_errno(lpSQLConn);
  12.     //mysql_error(lpSQLConn);
  13.  
  14.     MYSQL_RES* lpGetSQLRes = mysql_store_result(lpSQLConn);
  15.     mysql_free_result(lpGetSQLRes); // release sql memory
  16.  
  17.     return 0; // success
  18. }
  19.  
  20. void* ThreadSQL_HexWrite(void* lpGet)
  21. {
  22.  
  23.     LPThreadParam getParam = (LPThreadParam)lpGet;
  24.  
  25.     MYSQL* lpSQLConn = (MYSQL*)&getParam->lpSQLConn;
  26.     int iThreadIdx = getParam->iThreadIdx;
  27.  
  28.     printf("ID:%d\n", iThreadIdx);
  29.  
  30.     mysql_thread_init();
  31.  
  32.     lpSQLConn = mysql_init(NULL);
  33.  
  34.  
  35.     if (!mysql_real_connect(lpSQLConn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0))
  36.     {
  37.         ThreadSQLError(lpSQLConn, NULL);
  38.         return;
  39.     }
  40.     else
  41.     {
  42.         printf("mysql_real_connect OK!\n");
  43.     }
  44.  
  45.  
  46.     for (int i = 0; i < 1000000; i++)
  47.     {
  48.  
  49.         char lpCmdStr[8192] = "\0";
  50.         sprintf(lpCmdStr, "update %s set %s=0x%d where id=%d\0", "tb_Data", "Info", i, 1);
  51.  
  52.         if (ThreadExeSQL(lpSQLConn, (char*)lpCmdStr, strlen(lpCmdStr)))
  53.         {
  54.             MySQLError getError = ThreadSQLError(lpSQLConn, NULL);
  55.             HandleMySqlError(getError);
  56.  
  57.             return; //erroe
  58.         }
  59.         else
  60.         {
  61.             printf("ok. ");
  62.         }
  63.  
  64.         usleep(1000 * 10);
  65.     }
  66.  
  67.     mysql_close(lpSQLConn);
  68.  
  69.     mysql_thread_end();
  70.  
  71.  
  72.     printf("ThreadSQL_HexWrite OK!\n");
  73. }
  74.  
  75.  
  76. MYSQL* g_MySQLConnList[100];
  77.  
  78. void main()
  79. {
  80.  
  81.     if (mysql_library_init(0, NULL, NULL))
  82.     {
  83.         printf("could not initialize MySQL client library\n");
  84.         exit(1);
  85.     }
  86.  
  87.  
  88.     int thread_num = 1;
  89.  
  90.     //while (true)
  91.     {
  92.         pthread_t *pTh = new pthread_t[thread_num];
  93.  
  94.  
  95.         for (int i = 0; i < thread_num; i++)
  96.         {
  97.  
  98.             LPThreadParam lpSetParam = new ThreadParam;
  99.             lpSetParam->lpSQLConn = (MYSQL*)&g_MySQLConnList[i];
  100.             lpSetParam->iThreadIdx = i;
  101.  
  102.             printf("---create thread idx:%d\n", i);
  103.             if (0 != pthread_create(&pTh[i], NULL, ThreadSQL_HexWrite, lpSetParam))
  104.             {
  105.                 printf("pthread_create failed\n");
  106.                 continue;
  107.             }
  108.         }
  109.  
  110.         for (int i = 0; i < thread_num; i++)
  111.         {
  112.             pthread_join(pTh[i], NULL);
  113.         }
  114.  
  115.         delete[] pTh;
  116.     }
  117.  
  118.     mysql_library_end();
  119.  
  120.     printf("All Done!\n");
  121.  
  122. }
  123.  
May 23 '19 #1
Share this Question
Share on Google+
1 Reply


P: 2
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < 1000000; i++) 
  2. {
  3.  ......
  4.   if (ThreadExeSQL...)
  5.    { 
  6.       ...... 
  7.       continue; // this line change to continue; code copy with a little mistake. 
  8.     } 
  9.  
  10. }
  11.  
May 23 '19 #2

Post your reply

Sign in to post your reply or Sign up for a free account.