简体   繁体   English

MySQL createStatement()内存泄漏

[英]MySQL createStatement() memory leak

I´m running a big code with lots of MYSQL database access that and I´m noticing a memory leak that consumes the whole server memory after one day running. 我正在运行一个具有许多MYSQL数据库访问权限的大代码,并且注意到运行一天后,内存泄漏会占用整个服务器内存。

By isolating pieces I end up with the following test code: 通过隔离片段,我得到以下测试代码:

#include <iostream>
#include <string>
#include <sstream>
#include <thread>
#include <chrono>


#include "mysql_connection.h"

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

int main() {

    try
    {
        sql::Driver *driver = NULL;
        sql::Connection *connection = NULL;

        driver = get_driver_instance();

        connection = driver->connect("tcp://127.0.0.1:3306", "user", "pass");
        connection->setSchema("myschema");
        connection->setAutoCommit(true);

        while (true)
        {
            std::string sql("INSERT INTO tablename ('field1', 'field2', 'field3') VALUES ('1', '2', '3')");
            sql::Statement *stmt;
            stmt = connection->createStatement();
            stmt->execute(sql.c_str());
            stmt->close();
            std::cout << sql << std::endl;
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    }
    catch (std::exception& ex)
    {
        std::cout << "Error occurred: " << std::endl;
        std::cout << ex.what() << std::endl;
    }
    catch(...)
    {
        std::cout << "Unknown failure occurred." << std::endl;
    }
}

As the main while runs, the process memory usage starts increasing like 1Mb per minute. 作为主要的while运行,进程的内存使用量开始每分钟1Mb的一样增加。

Using valgrid --leak-check=full I got the following result after hitting to exit from the main loop: 使用valgrid --leak-check=full ,从主循环退出后得到以下结果:

==11988== Process terminating with default action of signal 2 (SIGINT)
==11988==    at 0x57F5D6D: recv (recv.c:29)
==11988==    by 0x5B1B022: vio_read (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.1.0)
==11988==    by 0x5B1B0A4: vio_read_buff (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.1.0)
==11988==    by 0x5AFA702: ??? (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.1.0)
==11988==    by 0x5AFA976: ??? (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.1.0)
==11988==    by 0x5AFB6A3: my_net_read (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.1.0)
==11988==    by 0x5AF151C: cli_safe_read (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.1.0)
==11988==    by 0x5AF2A15: ??? (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.1.0)
==11988==    by 0x5AF43A5: mysql_real_query (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.1.0)
==11988==    by 0x4F15BEE: sql::mysql::NativeAPI::LibmysqlStaticProxy::real_query(st_mysql*, char const*, unsigned long) (in /usr/lib/libmysqlcppconn.so.7.1.1.3)
==11988==    by 0x4F176F5: sql::mysql::NativeAPI::MySQL_NativeConnectionWrapper::query(sql::SQLString const&) (in /usr/lib/libmysqlcppconn.so.7.1.1.3)
==11988==    by 0x4F10579: sql::mysql::MySQL_Statement::do_query(sql::SQLString const&) (in /usr/lib/libmysqlcppconn.so.7.1.1.3)
==11988== 
==11988== HEAP SUMMARY:
==11988==     in use at exit: 333,479 bytes in 1,869 blocks
==11988==   total heap usage: 5,566 allocs, 3,697 frees, 1,100,674 bytes allocated
==11988== 
==11988== 144,880 bytes in 1,811 blocks are definitely lost in loss record 42 of 42
==11988==    at 0x4C2C12F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11988==    by 0x4EC4334: sql::mysql::MySQL_Connection::createStatement() (in /usr/lib/libmysqlcppconn.so.7.1.1.3)
==11988==    by 0x4011B7: main (test.cpp:32)
==11988== 
==11988== LEAK SUMMARY:
==11988==    definitely lost: 144,880 bytes in 1,811 blocks
==11988==    indirectly lost: 0 bytes in 0 blocks
==11988==      possibly lost: 0 bytes in 0 blocks
==11988==    still reachable: 188,599 bytes in 58 blocks
==11988==         suppressed: 0 bytes in 0 blocks
==11988== Reachable blocks (those to which a pointer was found) are not shown.
==11988== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==11988== 
==11988== For counts of detected and suppressed errors, rerun with: -v
==11988== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

So, am I doing something wrong or is this a know MYSQL bug ? 那么,我是在做错什么还是这是一个已知的MYSQL错误? How to fix the code and/or mySQL ? 如何修复代码和/或mySQL?

I´m running MySql 5.6.27-0ubuntu1 running on Ubuntu 15.10. 我正在运行在Ubuntu 15.10上运行的MySql 5.6.27-0ubuntu1。

You're missing delete stmt . 您缺少delete stmt Better would be to use a std::unique_ptr<sql::Statement> . 最好使用std::unique_ptr<sql::Statement>

(See eg https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-examples-query.html ) (请参见例如https://dev.mysql.com/doc/connector-cpp/zh-CN/connector-cpp-examples-query.html

Try 尝试

std::unique_ptr<sql::Statement> stmt(connection->CreateStatement());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM