Any tool to find size of memory allocated dynamica

2019-03-01 09:19发布

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

4条回答
我欲成王,谁敢阻挡
2楼-- · 2019-03-01 09:44

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:

#define malloc(s)    mymalloc(s)
#define realloc(p,s) myrealloc(p,s)
#define free(p)      myfree(p)

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.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-03-01 09:57

I would recommend the following:

  1. It you do have access to the source code that you wish to analyze, replace all malloc/realloc calls with calls you your OWN function that would perform the analysis.
  2. If you don't have access to the source code, you can use the Detours library from Microsoft. The library intercepts calls to arbitrary functions and redirects them to the custom-made implementation. In this implementation you can perform the analysis and then fall back to standard malloc/realloc.
查看更多
beautiful°
4楼-- · 2019-03-01 09:57

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.

查看更多
仙女界的扛把子
5楼-- · 2019-03-01 10:01

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.

查看更多
登录 后发表回答