Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
135 lines (97 sloc) 4.96 KB
Heapy
=====
Heapy is a very simple heap profiler (or memory profiler) for windows
applications.
It lets you see what parts of an application are allocating the most memory.
Heapy supports 32 and 64 bit applications written in C/C++. You do not need to
modify your application in any way to use Heapy.
Heapy will hook and profile any `malloc` and `free` functions it can find. This
will in turn cause `new` and `delete` to be profiled too (at least on MSVC `new`
and `delete` call `malloc` and `free`.
Usage
-----
To profile an application simply run Heapy.exe with the first argument as the
path to the exe you wish to profile. Make sure that the debug database (`.pdb`
file) is in the same directory as your target application so that you get symbol
information in your stack traces. You can profile release builds but profiling
debug or unoptimised builds gives the nicest stack traces.
Heapy_x64.exe C:\Windows\SysWOW64\notepad.exe
By default Heapy will run the given executable from the same folder as that
executable. You can specify a working directory with an optional second
argument:
Heapy_x64.exe C:\Windows\SysWOW64\notepad.exe C:\A\Working\Dir
Remember to call `Heapy_x64.exe` to profile 64 bit applications and
`Heapy_Win32.exe` to profile 32 bit applications.
Results
-------
Once your application is running Heapy will start writing profiling results to
the `Heapy_Profile.txt` file in the applications working directory.
Every 10 seconds and on the termination of your program information will be
added to the report.
Currently the report is very simple. Allocations are collated on a per stack
trace basis. Each time we add information to the report we simply write out the
top 25 allocating stack traces and the amount of memory they allocated.
Note that Heapy always *appends* to a report. You will have to delete/rename
`Heapy_Profile.txt` or just scroll to the bottom when repeatedly profiling.
Example
-------
This tiny test program `TestApplication`:
#include <windows.h>
#include <iostream>
void LeakyFunction(){
malloc(1024*1024*5); // leak 5Mb
}
void NonLeakyFunction(){
auto p = malloc(1024*1024); // allocate 1Mb
std::cout << "TestApplication: Sleeping..." << std::endl;
Sleep(15000);
free(p); // free the Mb
}
int main()
{
std::cout << "TestApplication: Creating some leaks..." << std::endl;
for(int i = 0; i < 5; ++i){
LeakyFunction();
}
NonLeakyFunction();
std::cout << "TestApplication: Exiting..." << std::endl;
return 0;
}
Gave the following two reports in `Heapy_Profile.txt` after being run with heapy:
=======================================
Printing top allocation points.
< Trimmed out very small allocations from std::streams >
Alloc size 1Mb, stack trace:
NonLeakyFunction e:\sourcedirectory\heapy\testapplication\main.cpp:9 (000000013FEC1D7E)
main e:\sourcedirectory\heapy\testapplication\main.cpp:22 (000000013FEC1E0D)
__tmainCRTStartup f:\dd\vctools\crt_bld\self_64_amd64\crt\src\crt0.c:241 (000000013FEC67FC)
BaseThreadInitThunk (00000000779A652D)
RtlUserThreadStart (0000000077ADC541)
Alloc size 25Mb, stack trace:
LeakyFunction e:\sourcedirectory\heapy\testapplication\main.cpp:6 (000000013FEC1D5E)
main e:\sourcedirectory\heapy\testapplication\main.cpp:20 (000000013FEC1E06)
__tmainCRTStartup f:\dd\vctools\crt_bld\self_64_amd64\crt\src\crt0.c:241 (000000013FEC67FC)
BaseThreadInitThunk (00000000779A652D)
RtlUserThreadStart (0000000077ADC541)
Top 13 allocations: 26.005Mb
Total allocations: 26.005Mb (difference between total and top 13 allocations : 0Mb)
=======================================
Printing top allocation points.
< Trimmed out very small allocations from std::streams >
Alloc size 25Mb, stack trace:
LeakyFunction e:\sourcedirectory\heapy\testapplication\main.cpp:6 (000000013FEC1D5E)
main e:\sourcedirectory\heapy\testapplication\main.cpp:20 (000000013FEC1E06)
__tmainCRTStartup f:\dd\vctools\crt_bld\self_64_amd64\crt\src\crt0.c:241 (000000013FEC67FC)
BaseThreadInitThunk (00000000779A652D)
RtlUserThreadStart (0000000077ADC541)
Top 5 allocations: 25.005Mb
Total allocations: 25.005Mb (difference between total and top 5 allocations : 0Mb)
The first allocation report shows stack traces for both the leaky and non leaky
alloc - it was taken before the non leaky alloc was freed so shows that 1Mb as
in use. Note that the LeakyFunction allocation size was taken as the sum of all
the calls to it from the loop. Also note that the LeakyFuncion alloc is the only
allocation shown by the final report (which is generated on application exit)
since these mallocs were never cleaned up!
You should be able to run the the ProfileTestApplication_Win32.bat or
ProfileTestApplication_x64.bat to generate a Heapy_Profile.txt similar to
the above!