Memory leakage of MySQLC client

Source: Internet
Author: User
Tags valgrind
MySQLC client memory leakage problem Using valgrind test MySQL C client mysqlclient found that it will be reported by valgrind in normal use memory leakage.

1. normal use cases

The following code is the most common code for reading data using mysqlclient

#include 
 
  #include 
  
   int main(){MYSQL *conn;MYSQL_RES *result;MYSQL_ROWrow;char*w;conn = mysql_init(NULL);mysql_real_connect(conn, "127.0.0.1", "root", "", "db_test", 3306, NULL, 0);mysql_query(conn, "select id from test");result = mysql_store_result(conn);if (result == NULL) {printf("%d:%s/n", mysql_errno(conn), mysql_error(conn));goto out;}while ((row = mysql_fetch_row(result))) {w = row[0];} out:mysql_free_result(result);mysql_close(conn);mysql_library_end();return 0;}
  
 

The above code uses valgrind to detect memory leakage. the output is as follows:

Cobbliu @ ubuntu :~ /Dev/test $ gcc-o mysql. c-lmysqlclientcobbliu @ ubuntu :~ /Dev/test $ valgrind -- leak-check = full -- show-reachable = yes. /mysql = 4497 = Memcheck, a memory error detector = 4497 = Copyright (C) 2002-2011, and gnu gpl 'd, by Julian Seward et al. = 4497 = Using Valgrind-3.7.0 and LibVEX; rerun with-h for copyright info = 4497 = Command :. /mysql = 4497 = 4497 = 4497 = 4497 = heap summary: = in use at exit: 73,872 bytes in 21 blocks = 4497 = total heap usage: 84 allocs, 63 frees, 128,626 bytes allocated = 4497 ==## some output is ignored here #== 4497 === leak summary: = 4497 === definitely lost: 0 bytes in 0 blocks = 4497 = indirectly lost: 0 bytes in 0 blocks = 4497 = possibly lost: 0 bytes in 0 blocks = 4497 = still reachable: 73,872 bytes in 21 blocks = 4497 = suppressed: 0 bytes in 0 blocks = 4497 = 4497 = For counts of detected and suppressed errors, rerun: -v = 4497 = error summary: 0 errors from 0 contexts (suppressed: 0 from 0)

It can be seen that some of mysqlclient exists in normal use cases.mysql_close()And is not released.

2 Solution

An article on stackoverflow puts forward a solution:mysql_close()Call latermysql_library_end()To release the remaining memory space.mysql_library_end()Is described as follows:

This function finalizes the MySQL library.Call it when you are done using the library (for example, after disconnecting from the server).The action taken by the call depends on whether your application is linked to the MySQL client library or the MySQL embedded server library.For a client program linked against the libmysqlclient library by using the -lmysqlclient flag, mysql_library_end() performs some memory management to clean up.

3. performance test

Addmysql_library_end()Function:

//codes mysql_free_result(result);mysql_close(conn); mysql_library_end();// codes

Then valgrind is used to detect memory leakage:

cobbliu@ubuntu:~/dev/test$ valgrind --leak-check=full --show-reachable=yes ./mysql==4513== Memcheck, a memory error detector==4513== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.==4513== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info==4513== Command: ./mysql==4513====4513====4513== HEAP SUMMARY:==4513== in use at exit: 0 bytes in 0 blocks==4513== total heap usage: 84 allocs, 84 frees, 128,626 bytes allocated==4513====4513== All heap blocks were freed -- no leaks are possible==4513====4513== For counts of detected and suppressed errors, rerun with: -v==4513== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

We can see that there is no unreleased memory.

Therefore, if the daemon process is called frequently mysql_init And mysql_close In mysql_close Call later mysql_library_end() To release unreleased memory.

Author: CobbLiu

Date: 13:42:21 CST

HTML generated by org-mode 6.33x in emacs 23

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.