检查正在运行的程序中的内存泄漏 [英] Checking for memory leaks in a running program

查看:204
本文介绍了检查正在运行的程序中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



作为使用 valgrind 经常检查我的代码中的内存泄漏在过去一两年,我突然想到,它只检测到程序的生命之后的丢失/不自由的记忆。因此,有鉴于此,我认为如果您有一个长期运行的程序,其中 malloc()的间歇性,并且直到应用程序退出,然后才可以使用内存(不一定是通过泄漏),这是巨大的,不可观察到使用这些工具,因为他们只检查程序的生命周期。是否有类似GDB的工具,可以在运行时停止应用程序,并检查在应用程序的生活中的实例是否被引用的内存?

解决方案


是否有类似GDB的工具,可以在运行时停止应用程序,并检查在实例中引用的内存在应用程序的生命中?


是:Valgrind。



具体,SVG版本的Valgrind在其中嵌入了一个gdbserver存根。



这允许您进行各种不错的调试,以前不可能:




  • 您可以在valgrind 下运行程序,同时具有GDB断点

  • 您可以问valgrind:这个内存是分配的吗?这个变量是否已初始化?

  • 您可以询问valgrind:自上次发生泄漏以来发生了什么新的泄漏?



我认为你也可以要求它列出不泄露的新分配。


I have a question out of curiosity relating to checking for memory leaks.

Being someone who has used valgrind frequently to check for memory leaks in my code for the last year or two, I suddenly came to thinking that it only detects lost/unfreed memory after the life of the program.

So, in light of that, I was thinking that if you have a long-running program which malloc()'s intermittently and doesn't free() until the application exits, then the potential to eat memory (not necessarily through leaks) is huge and isn't observable using these tools because they only check after the programs lifetime. Are there GDB-like tools which can stop an application while running and check for memory which is and isn't referenced at an instance in the life of the application?

解决方案

Are there GDB-like tools which can stop an application while running and check for memory which is and isn't referenced at an instance in the life of the application?

Yes: Valgrind.

Specifically, the SVN version of Valgrind has a gdbserver stub embedded into it.

This allows you to do all kinds of cool debugging, not possible before:

  • You can run program under valgrind and have GDB breakpoints at the same time
  • You can ask valgrind: is this memory allocated? was this variable initialized?
  • You can ask valgrind: what new leaks happened since last time I asked for leaks?

I think you can also ask it to list not-leaked new allocations.

这篇关于检查正在运行的程序中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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