如何确定是哪个 C/C++对象占用最多的内存

发布于 2024-08-09 07:04:07 字数 234 浏览 10 评论 0原文

我有一个混合模式应用程序(托管和本机),它具有较高的内存占用量。我已经发现大部分内存是由本机代码分配的。我说的不是内存泄漏,而是程序启动后很早发生的高内存消耗,然后相对稳定。

您是否知道有任何工具可以显示哪些 C/C++ 对象使用最多的内存? 我已经尝试过 DebugDiag 1.1 和 SoftwareVerify 的内存验证器,但这两个工具都没有提供足够的信息来识别 C/C++ 对象。

问候

弗兰克

I have a mixed mode application (managed and native) which has a high memory footprint. I already have found out that most of the memory is allocated by native code. I am not talking about a memory leak, but about a high memory consumption that occurs very early after the program starts and then is relatively stable.

Are you aware of any tool that shows you which C/C++ objects use the most memory?
I have already tried DebugDiag 1.1 and SoftwareVerify's Memory Validator, but both tools do not give enough information to identify the C/C++ objects.

Regards

Frank

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

待"谢繁草 2024-08-16 07:04:07

AQTime 的内存分析器对此非常有效。它是我尝试过的少数几个使用同一分析器处理本机代码和托管代码的分析器之一,包括支持混合模式程序集。

AQTime's memory profiler works well for this. It's one of the few profilers I've tried that handles both native and managed code using the same profiler, including supporting mixed mode assemblies.

奶气 2024-08-16 07:04:07

我可以建议一种更“硬核”的方法吗?

WinDbg 的 !heap 命令可以揭示有关本机堆的许多重要信息。
首先执行以下步骤:

A. 启动 GFlags,转到“图像文件”选项卡,输入进程的名称并按 Tab 键。

B. 按“启用页堆”和“创建用户模式堆栈跟踪数据库”,然后按确定。

C. 开始你的流程。

执行上述步骤将告诉 Windows 收集有关您的进程的内存分配信息。我们稍后将使用此信息。

重要提示:收集此信息将使您的应用程序使用更多内存,并且可能会变慢。 Windows 将在您每次运行进程时继续收集此信息,直到您通过启动 GFlags 并删除您的选择来告诉它。

将 WinDbg 连接到您的应用程序并设置正确的符号。除了您自己的符号之外,您还需要 Microsoft 的符号。使用 .symfix 命令,然后使用 .reload /f 使 WinDbg 从 Microsoft 的符号服务器下载正确的符号(可能需要几分钟)。

设置完所有符号后,执行以下步骤:

A. !heap -stat - 查看所有进程堆的使用情况摘要

B. 选择一个堆进行检查。如果您正在寻找大对象,那么具有最高提交字节数的对象将是一个不错的选择。

C. !heap -stat -h “堆句柄” - 查看堆的分配统计信息。在输出中,您将发现为每个分配大小分配了多少块。

D. 选择较高的分配大小之一并使用 !heap -flt s "size" 转储相同大小的所有堆条目。

E. !heap -p -a "UserPtr" 将打印分配堆栈(以及其他信息)。如果您不使用 GFlags 设置“启用页堆”,则此信息将不可用。

就是这样,使用调用堆栈中的信息并查看源代码来识别那些大对象。

顺便说一句,

如果您尚未安装 Windows 调试工具包,您可以从 这里

也许这个方法并不像你想象的那么简单,但它确实有效:)
玩得开心。

Can I suggest a more "hardcore" approach?

WinDbg's !heap command can reveal a lot of important information about the native heap.
start by doing the following steps:

A. launch GFlags, go to the Image File tab, type your process's name and press tab.

B. press the "Enable page heap" and "Create User mode stack trace database" and press OK.

C. start your process.

Doing the above steps will tell windows to collect memory allocation information about your process. we will use this information later.

IMPORTANT : collecting this information will make your application use more memory and probably to be slower. windows will continue collecting this information every time you'll run your process until you;ll tell it otherwise by launching GFlags and remove your selections.

attach WinDbg to your application and set the correct symbols. beside your own symbols, you'll need Microsoft's symbols. use the .symfix command and then use .reload /f to make WinDbg download the correct symbols from microsoft's symbol server (it can take several minutes).

After all the symbols are set perform the following steps:

A. !heap -stat - to see a usage summery of all your process's heaps

B. choose one heap to examine. the one with the highest committed bytes will be a good candidate if you are looking for big objects.

C. !heap -stat -h "heap handle" - to see the allocation statistics for the heap. in the output you will find how many blocks are allocated for each allocation size.

D. pick one of the higher allocation sizes and use !heap -flt s "size" to dump all the heap entries of the same size.

E. !heap -p -a "UserPtr" will print the allocation stack (along with other information). this information will not be available if you won't set the "Enable page heap" using GFlags.

That's it, use the information from the call stack and look at your source code to identify those big objects.

B.T.W

If you don't have the Debugging Tools for Windows package installed already, you can download it from here.

Maybe this approach is not as simple as you expected but it works :)
Have fun.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文