混合模式项目的内存泄漏检测:托管、非托管和本机
我有一个 Visual Studio 2010 解决方案,其中包含 C#(托管)、C++/CLI(非托管)和纯 C++(本机)项目。我想在所有 3 个项目中或至少在本机代码周围执行内存泄漏检测:
- C# 项目引用非托管 dll(我可以访问常用的 .NET 内存分析工具,因此在其上运行内存分析并不是真正的问题它)。
- C++/CLI 是原生 C++ 库的一个非常薄包装器,因此我实际上不需要对其进行分析(不用担心)。
- C++ 本机代码是最难分析的代码。
我尝试过使用 Intel Inspector XE 2011,但它太慢了...做一些简单的事情,例如初始化我的系统,需要很长时间,以至于我什至还没有看到它完成。当我在没有 IXE 2011 的情况下运行系统时,初始化系统所需的时间不超过 10-15 秒,而使用 IXE 时,我们让它运行几个小时,并且无法完成初始化。我试图排除某些库的分析,但它完全没有效果。
我尝试过使用 Visual Leak Detector,但完成运行后它说找不到任何内存泄漏。我对这个结果感到怀疑,所以我故意在一个经常运行的函数中放置了一段代码,以确保存在内存泄漏:
int* memoryLeak = new int;
我再次运行 VLD,但它输出了相同的消息。我正在考虑覆盖 new
/delete
运算符,甚至只是 malloc
/free
,但我想在我深入研究之前,请确保我已经用尽了所有其他选项。
如何使用 Visual Studio 2010 分析本机 C++ 库的内存使用情况?是否还有其他可能有用的工具或技术(即使它们不与 VS2010 集成)?
I have a Visual Studio 2010 solution that contains C# (managed), C++/CLI (unmanaged) and pure C++ (native) projects. I would like to perform memory leak detection across all 3 projects or at least around the native code:
- The C# project references the unmanaged dll (I have access commonly available .NET Memory Profiling tools, so it's not really a problem to run memory profiling on it).
- The C++/CLI is a very thin wrapper around the native C++ library, so I don't really need to profile it (not that worried about it).
- The C++ native code is the one that's the most difficult to profile.
I've tried using Intel Inspector XE 2011, but it's simply too slow... doing a simple thing like merely initializing my system and takes so long that I haven't even seen it complete yet. When I run my system without IXE 2011, it takes me no more than 10-15 seconds to initialize my system, while with IXE we've let it run for hours and it doesn't get past initialization. I've attempted to exclude certain libraries from being profiled, but it had absolutely no effect.
I've tried using the Visual Leak Detector, but after completing the run it said that it couldn't find any memory leaks. I was suspicious about that result so I intentionally placed a piece of code in a frequently run function to ensure that there is a memory leak:
int* memoryLeak = new int;
I ran with VLD again, but it spit out the same message. I'm considering overriding the new
/delete
operators or even just the malloc
/free
, but I wanted to make sure that I've exhausted all other options before I delve into doing that.
What can I do to profile the memory usage of my native C++ library with Visual Studio 2010? Are there any other tools or techniques that might work (even if they don't integrate with VS2010)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 .NET 中,即使您使用托管对象,也可能有一些东西永远不会被处理(请在此处查看一些示例:C# 中的内存泄漏)。
关于本机部分,您可以使用两种不同的方法:
使用不同的内存分析器软件,此处列出了许多软件:Windows 有一个好的 Valgrind 替代品吗?
改变你的使用 debug malloc/new 并打印代码中完成分配的位置的源:http://www.flipcode.com/archives/Detecting_Memory_Leaks.shtml
In .NET even if you use managed objects there may be something that never get disposed (check some examples here: Memory Leak in C#).
About the native part, you may use two different approaches:
use a different memory profiler software, many listed here: Is there a good Valgrind substitute for Windows?
change your sources to use debug malloc/new and to print where in the code the allocations are done: http://www.flipcode.com/archives/Detecting_Memory_Leaks.shtml
进行单元测试来测试单元的内存泄漏:
http://msdn.microsoft.com/en-us/library/x98tx3cf.aspx
在分配对象之前获取第一个内存状态,在释放对象之后获取第二个内存状态。之后比较你的记忆状态。
您也可以尝试使用其他分析器,例如 valgrind、devpartner。
Make unit tests which will test the units for memory leaks:
http://msdn.microsoft.com/en-us/library/x98tx3cf.aspx
Get the first memory state before allocating your object and the second after release it. Compare your memory states after.
Also you can try to use other profilers like valgrind, devpartner.