调试内存泄漏问题,而没有任何工具 [英] Debugging memory leak issues without any tool

查看:151
本文介绍了调试内存泄漏问题,而没有任何工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

记者? - 如果你没有工具来检查你将如何检测内存泄漏问题

Interviewer - If you have no tools to check how would you detect memory leak problems?

答案 - 我会读code,看看是否所有我所分配的内存已经在$ C $被释放由我C本身

Answer - I will read the code and see if all the memory I have allocated has been freed by me in the code itself.

记者并不满​​足。是否有任何其他的方式来做到这一点?

Interviewer wasn't satisfied. Is there any other way to do so?

推荐答案

对于所有下面定义的实现,需要写的malloc ()及包装; 免费()函数。

For all the implementation defined below, one needs to write wrappers for malloc() & free() functions.


  1. 为了简单起见,不断的malloc计数()及跟踪;自由()。如果没有,那么等于你有一个内存泄漏。

  1. To keep things simple, keep track of count of malloc() & free(). If not equal then you have a memory leak.

一个更好的版本将是跟踪地址的malloc()ED&安培; free()的编这样你可以找出哪些地址是的malloc()主编,但不是免费的()主编。但是,这一次,将也无能为力,因为你可以不涉及地址源$ C ​​$ C,特别是它成为一个挑战,当你有一个大的源$ C ​​$ C。

A better version would be to keep track of the addresses malloc()'ed & free()'ed this way you can identify which addresses are malloc()'ed but not free()'ed. But this again, won't help much either, since you can't relate the addresses to source code, especially it becomes a challenge when you have a large source code.

所以在这里,你可以添加一个功能更给它。对于如我写了FreeBSD的内核类似的工具,你可以修改的malloc ()调用存储模块/文件信息(给每个模块/文件一个没有,你在一些标题#定义它),在堆栈跟踪函数调用导致这种的malloc ()和商店它在数据结构,沿着边上述信息每当的malloc ()或免费()被调用。通过使用的malloc ()返回的地址来匹配它免费()。所以,当他们的内存泄漏,您有哪些地址没有免费()'编在哪个文件,什么是所谓的确切功能(通过<$ C信息$ C>堆栈跟踪),以针点吧。

So here, you can add one more feature to it. For eg, I wrote a similar tool for FreeBSD Kernel, you can modify the malloc() call to store the module/file information (give each module/file a no, you can #define it in some header), the stack trace of the function calls leading to this malloc() and store it in a data structure, along side the above information whenever a malloc() or free() is called. Use addresses returned by malloc() to match with it free(). So, when their's a memory leak, you have information about what addresses were not free()'ed in which file, what were the exact functions called (through the stack trace) to pin point it.

这样的,这个工具是工作,在一个崩溃的,我用来获取核心转储。我曾定义全局(这个数据结构我在那里收集数据)在内核内存空间,我可以访问使用 GDB 和检索信息。

The way, this tool worked was, on a crash, I used to get a core-dump. I had defined globals (this data structure where I was collecting data) in kernel memory space, which I could access using gdb and retrieve the information.

编辑:

近日在调试Linux内核一个memeory泄漏,我碰到这个工具叫 kmemleak 它实现了我上面点#3所描述的类似的算法。在这里的基本算法部分阅读: https://www.kernel.org/doc/Documentation/kmemleak.txt

Recently while debugging a memeory leak in linux kernel, I came across this tool called kmemleak which implements a similar algorithm I described in point#3 above. Read under the Basic Algorithm section here: https://www.kernel.org/doc/Documentation/kmemleak.txt

这篇关于调试内存泄漏问题,而没有任何工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆