473,387 Members | 3,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
1 1889
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

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

Similar topics

0
by: Kayra Otaner | last post by:
Hi all, I want to get your opinions on how to increase available/free memory and performance on a heavy volume database server. I have MySQL 4.0.13 running on RH 7.2 replicated to another RH...
0
by: kayra | last post by:
Hi all, I want to get your opinions on how to increase available/free memory and performance on a heavy volume database server. I have MySQL 4.0.13 running on RH 7.2 replicated to another RH...
2
by: Brett B | last post by:
I just installed mysql on linux. If I open a terminal, su to root, then type "mysql", I am able to connect to the server and run my queries. If I exit out of su so that I am my own id (baisley)...
4
by: Bob Bedford | last post by:
We have no access to a mysql NG on my provider's server, so we ask here: We have a long query (long in text) with a UNION between 2 select. We have been informed that some times the query...
1
by: astroboy | last post by:
How do I detect which object cause memory leak???? Thx
1
by: linux | last post by:
Sorry if this is a really dumb question. I am trying to connect to our remote MySQL server (which is running just fine). I have a perl script which worked just fine on my other Fedora 4 box (all...
2
by: saran | last post by:
I am having a problem with MySQL consuming a lot of memory and eventually throwing an Out of Memory error and restarting itself. The symptoms are that swap usage continues to rise until some...
3
by: dan | last post by:
Hi all, I have a question about flash-memory lifespan. My database consists of 1 table, the table has 50 fields of 10 bytes each, thus each record will have 500bytes. There are 1000 records in...
5
by: Andreas Schmitt | last post by:
Hi, I recently worked on an open source project and tried to make on of the arrays they are using dynamically allocated to get rid of the max size. I used the realloc instead of the usual C++...
6
by: markodilore | last post by:
Hey Guys, you helped me once when I tryied to create a database : "Access denied for user ''@'localhost' ". On my Mac OS 10.4, I had no problem creating database and modifying it from the terminal....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.