有什么工具可以查找使用 malloc/realloc 动态分配的内存大小吗?
我有一个包含所有 C 代码的 MS-Visual Studio 2005 工作区。该应用程序(exe)使用 malloc 和 realloc 从堆动态分配内存。当我运行特定的测试用例时,我想计算此应用程序使用 malloc/realloc 在堆上分配的最大分配大小。
我不想通过记录 malloc 大小并累积它们来更改代码,因为:
a) 可能存在一种情况,即 1KB 的内存被 malloc'ed,然后释放,然后 2KB 的内存被 malloc'ed 。所以最大值是 2KB,我需要将其作为值而不是 1+2=3KB。
所以我必须真正了解此代码中发生的所有 malloc/free 并为此添加代码,这是我想避免的。
1)那么是否有任何工具(免费软件/许可)可以查找使用 malloc/realloc 动态分配的最大或总内存的大小?
2)MS Visual Studio 2005/2008本身是否提供此类功能?
谢谢,
-AD
I have a MS-Visual Studio 2005 workspace having all c code. This application(exe) allocates memory dynamically from heap using malloc and realloc. I want to calculate the maximum size allocated size allocated on heap using malloc/realloc by this application program when i run particular test case.
I do not want to change the code by noting the malloc sizes and accumulating them, because:
a) there can be a scenario, that some memory of 1KB is malloc'ed, then freed, and then a memory of 2KB is malloc'ed. So max is 2KB, which i need to get as the value and not 1+2=3KB.
So i have to really see whereall malloc/free is happening in this code and add code for this, which i want to avoid.
1) So are there any tools(freeware/licensed) to find size of maximum or total memory allocated dynamically using malloc/realloc?
2)Does MS Visual Studio 2005/2008 itself provide anything of this sort?
thanks,
-AD
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果静态链接 CRT,则可以“否决”malloc、realloc、free 的实现(事实上,CRT 中 malloc.c、realloc、c free.c 和/或 dbgheap.c 中出现的所有函数) 。这是可行的,但可能需要一些迭代才能获得需要被否决的完整函数集。
如果与 CRT 动态链接,则可以像这样重新定义 malloc、realloc 和 free:
mymalloc、myrealloc 和 myfree 的实现可以简单地使用 malloc、realloc 和 free(确保不要使用源文件中的#define)实现 mymalloc,...)或者您可以使用本机 Windows 函数。
If you statically link with the CRT, you can 'overrule' the implementation of malloc, realloc, free (in fact, all functions that appear in malloc.c, realloc,c free.c and/or dbgheap.c in the CRT). It is doable but may require some iterations before you get the full set of functions that need to be overruled.
If you dynamically link with the CRT, you can redefine malloc, realloc and free like this:
The implementations of mymalloc, myrealloc and myfree can then simply use malloc, realloc and free (be sure not to use the #define in the source file that implements mymalloc, ...) or you could use the native Windows functions.
内存验证器可以做到这一点。
有几种不同的报告对您很有用:
运行总计。它显示为一个对话框,提供每个主内存分配器(C 运行时、HeapAlloc、LocalAlloc、GlobalAlloc、CoTaskMemAlloc 等)的当前值、累积值和总计值。
对象。这是主要选项卡之一,显示对象类型、大小、计数、累积。
还有每个线程和每个 dll 值的子选项卡。
尺寸。这是主要选项卡之一,显示大小、计数、累积。
还有每个线程和每个 dll 值的子选项卡。
虚拟。这将显示内存的图形视图(一个像素 == 一页内存),并具有显示虚拟内存页和虚拟内存段落的详细虚拟内存数据的子选项卡。
全面披露:我是内存验证器团队的一员。
Memory Validator can do this.
There are several different reports that you will find useful:
Running Totals. This is presented as a dialog box and provides current, cumulative and total values for each of the main memory allocator (C runtime, HeapAlloc, LocalAlloc, GlobalAlloc, CoTaskMemAlloc, etc).
Objects. This is one of the main tabs and displays object type, size, count, cumulative.
Also subtabs for per-thread and per-dll values.
Sizes. This is one of the main tabs and displays size, count, cumulative.
Also subtabs for per-thread and per-dll values.
Virtual. This displays a graphical view of memory (one pixel == one page of memory) and has subtabs showing detailed virtual memory data for virtual memory pages and virtual memory paragraphs.
Full disclosure: I am part of the Memory Validator team.
我建议如下:
malloc/realloc
调用替换为调用您自己的将执行分析的函数。malloc/realloc
。I would recommend the following:
malloc/realloc
calls with calls you your OWN function that would perform the analysis.malloc/realloc
.VS 有许多堆调试工具,例如
_heapwalk
,它可以让您遍历堆并获取有关堆上块的信息。您需要做的大部分事情是弄清楚堆何时达到最大使用率,这样您就知道何时遍历它并找到其大小。VS has a number of heap debugging tools such as
_heapwalk
, which will let you walk through the heap and get information about blocks on the heap. Most of what you need to do is figure out when your heap is at maximum usage, so you know when to walk it and find its size.