MySQL C 客户端的内存泄漏问题
我们的一个服务器软件在线上环境运行时出现了内存缓慢增长的问题。
用valgrind测试 MySQL的C客户端mysqlclient发现,它在正常的使用中会被valgrind报出存在内存泄漏。
1 正常使用场景
下面的代码是使用mysqlclient读取数据的最常用的代码
#include <mysql/mysql.h>
#include <stdio.h> int main()
{
MYSQL *conn;
MYSQL_RES *result;
MYSQL_ROW row;
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;
}
上面的代码用valgrind检测内存泄漏输出如下:
cobbliu@ubuntu:~/dev/test$ gcc -o mysql mysql.c -lmysqlclient
cobbliu@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== HEAP SUMMARY:
==4497== in use at exit: 73,872 bytes in 21 blocks
==4497== total heap usage: 84 allocs, 63 frees, 128,626 bytes allocated
==4497==
#
# 这儿忽略了一部分输出
#
==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 with: -v
==4497== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
可以看出,在正常的使用场景下,mysqlclient有一部分内存在 mysql_close() 之后并没有被释放。
2 解决方法
stackoverflow上的一篇文章提出了解决方法:在 mysql_close() 之后调用 mysql_library_end() 来释放 剩余的内存空间 MySQL开发手册对 mysql_library_end() 的描述如下:
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 效果检验
在上面的示例代码中加入 mysql_library_end() 函数:
//codes
mysql_free_result(result);
mysql_close(conn);
mysql_library_end();
// codes
然后用valgrind检测内存泄漏:
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)
可以看出,已经没有未释放的内存了。
所以对于daemon进程,如果频繁地调用 mysql_init 和 mysql_close 的话,记得在 mysql_close 之后调用 mysql_library_end() 来释放未被释放的内存
Author: CobbLiu <cobblau@gmail.com>
Date: 2014-05-05 13:42:21 CST
HTML generated by org-mode 6.33x in emacs 23
MySQL C 客户端的内存泄漏问题的更多相关文章
- 上Mysql com.mysql.jdbc.StatementImpl$CancelTask内存泄漏问题和解决方法
近来在负责公司短信网关的维护及建设,随着公司业务发展对短信依赖越来越严重了,短信每天发送量也比曾经每天40多w发送量暴增到每天达到200w发送量.由于是採用Java做发送底层,压力递增情况下不可避免的 ...
- 使用 Android Studio 检测内存泄漏与解决内存泄漏问题
本文在腾讯技术推文上 修改 发布. http://wetest.qq.com/lab/view/63.html?from=ads_test2_qqtips&sessionUserType=BF ...
- C++程序内存泄漏检测方法
一.前言 在Linux平台上有valgrind可以非常方便的帮助我们定位内存泄漏,因为Linux在开发领域的使用场景大多是跑服务器,再加上它的开源属性,相对而言,处理问题容易形成“统一”的标准.而在W ...
- 了解 JavaScript 应用程序中的内存泄漏
简介 当处理 JavaScript 这样的脚本语言时,很容易忘记每个对象.类.字符串.数字和方法都需要分配和保留内存.语言和运行时的垃圾回收器隐藏了内存分配和释放的具体细节. 许多功能无需考虑内存管理 ...
- C/C++内存泄漏及检测 转
C/C++内存泄漏及检测 2011-02-20 17:51 by 吴秦, 30189 阅读, 13 评论, 收藏, 编辑 “该死系统存在内存泄漏问题”,项目中由于各方面因素,总是有人抱怨存在内存泄漏, ...
- JS内存泄漏 和Chrome 内存分析工具简介(摘)
原文地址:http://web.jobbole.com/88463/ JavaScript 中 4 种常见的内存泄露陷阱 原文:Sebastián Peyrott 译文:伯乐在线专栏作者 - AR ...
- Java内存泄漏分析与解决方案
Java内存泄漏是每个Java程序员都会遇到的问题,程序在本地运行一切正常,可是布署到远端就会出现内存无限制的增长,最后系统瘫痪,那么如何最快最好的检测程序的稳定性,防止系统崩盘,作者用自已的亲身经历 ...
- 调不尽的内存泄漏,用不完的Valgrind
调不尽的内存泄漏,用不完的Valgrind Valgrind 安装 1. 到www.valgrind.org下载最新版valgrind-X.X.X.tar.bz2 2. 解压安装包:tar –jxvf ...
- VC使用CRT调试功能来检测内存泄漏
信息来源:csdn C/C++ 编程语言的最强大功能之一便是其动态分配和释放内存,但是中国有句古话:“最大的长处也可能成为最大的弱点”,那么 C/C++ 应用程序正好印证了这句话.在 C/C+ ...
随机推荐
- [翻译] FTCoreText
FTCoreText An open source Objective-C interface component that makes use of the CoreText framework t ...
- java通过System.getProperty获取系统属性
getProperties public static Properties getProperties() 确定当前的系统属性. 首先,如果有安全管理器,则不带参数直接调用其 checkProper ...
- apache基金会项目及甲骨文项目汇总
Apache软件基金会 顶级项目 ▪ ActiveMQ ▪ Ant ▪ Apache HTTP Server ▪ APR ▪ Beehive ▪ Camel ▪ Cassandra ▪ Cayenne ...
- 在 Sublime Text 2 下开启 Vim 模式
緣由 由於在 Sublime Text 2 下操作時會想起 Vim 下的鍵盤操作.一時興起在網絡上找了下,發現 Sublime Text 2 是支持類似 Vim 的鍵盤操作的.在此分享下配置過程. 打 ...
- SpringMVC处理方法的数据绑定
一.SpringMVC数据绑定流程 Spring MVC通过反射机制对目标处理方法的签名进行解析,将请求消息中的信息以一定的方式转换并绑定到处理方法的入参中.数据绑定的核心部件是DataBinder, ...
- Java – Top 5 Exception Handling Coding Practices to Avoid
This article represents top 5 coding practices related with Java exception handling that you may wan ...
- c# winform编程之多线程ui界面资源修改总结篇【转】
c# winform编程之多线程ui界面资源修改总结篇 单线程的winfom程序中,设置一个控件的值是很easy的事情,直接 this.TextBox1.value = "Hello Wor ...
- [leetcode]Sum Root to Leaf Numbers @ Python
原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...
- Style 的查找 FindResource
1)根据名称查找 PrintPreview fe = new PrintPreview(new Summary()); string strResourceHeader = "headerS ...
- Linux Shell处理文本最常用的工具大盘点
导读 本文将介绍Linux下使用Shell处理文本时最常用的工具:find.grep.xargs.sort.uniq.tr.cut.paste.wc.sed.awk:提供的例子和参数都是最常用和最为实 ...