Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. CentOS6.9 (MySql v5.7.22) use mysql C API mysql_real_query cause the memory always growing

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

Scheduled Pinned Locked Moved Database
helpannouncementdatabasemysqljson
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    normga
    wrote on last edited by
    #1

    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.

    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 lpSetPar
    
    J 1 Reply Last reply
    0
    • N normga

      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.

      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 lpSetPar
      
      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      Doesn't seem like the code is complete.

      normga wrote:

      MYSQL* g_MySQLConnList[100];

      That is a list of uninitialized pointers. Where do those pointers get set to actually point to something?

      N 1 Reply Last reply
      0
      • J jschell

        Doesn't seem like the code is complete.

        normga wrote:

        MYSQL* g_MySQLConnList[100];

        That is a list of uninitialized pointers. Where do those pointers get set to actually point to something?

        N Offline
        N Offline
        normga
        wrote on last edited by
        #3

        yes. g_MySQLConnList fill with like 'new MYSQL[100]'; In fact, in my testing environment the code always query mysql successful, but leak memory.

        J 1 Reply Last reply
        0
        • N normga

          yes. g_MySQLConnList fill with like 'new MYSQL[100]'; In fact, in my testing environment the code always query mysql successful, but leak memory.

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          Perhaps you missed my point. You have a list of pointers. Nothing more. The pointer must be set to point to something. Where does that happen?

          normga wrote:

          the code always query mysql successful

          That doesn't mean anything. Code can run successfully, sometimes, even with uninitialized pointers. It depends on how the memory is laid down.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups