views:

3636

answers:

12

Im trying to find a tool that can help me find out where my memory is allocated in a certain stage of my program. Most memory profilers cant do this and just tell you if memory is leaked or not.

Does any one know of a tool that can do this?

Edit:

OS: Win32

Visual studio 2005

+2  A: 

Check out Valgrind

Memcheck
Memcheck detects memory-management problems, and is aimed primarily at C and C++ programs. When a program is run under Memcheck's supervision, all reads and writes of memory are checked, and calls to malloc/new/free/delete are intercepted. As a result, Memcheck can detect if your program: Accesses memory it shouldn't (areas not yet allocated, areas that have been freed, areas past the end of heap blocks, inaccessible areas of the stack). Uses uninitialised values in dangerous ways. Leaks memory. Does bad frees of heap blocks (double frees, mismatched frees). Passes overlapping source and destination memory blocks to memcpy() and related functions. Memcheck reports these errors as soon as they occur, giving the source line number at which it occurred, and also a stack trace of the functions called to reach that line. Memcheck tracks addressability at the byte-level, and initialisation of values at the bit-level. As a result, it can detect the use of single uninitialised bits, and does not report spurious errors on bitfield operations. Memcheck runs programs about 10--30x slower than normal.

Cachegrind
Cachegrind is a cache profiler. It performs detailed simulation of the I1, D1 and L2 caches in your CPU and so can accurately pinpoint the sources of cache misses in your code. It identifies the number of cache misses, memory references and instructions executed for each line of source code, with per-function, per-module and whole-program summaries. It is useful with programs written in any language. Cachegrind runs programs about 20--100x slower than normal.

Callgrind
Callgrind, by Josef Weidendorfer, is an extension to Cachegrind. It provides all the information that Cachegrind does, plus extra information about callgraphs. It was folded into the main Valgrind distribution in version 3.2.0. Available separately is an amazing visualisation tool, KCachegrind, which gives a much better overview of the data that Callgrind collects; it can also be used to visualise Cachegrind's output.

Massif
Massif is a heap profiler. It performs detailed heap profiling by taking regular snapshots of a program's heap. It produces a graph showing heap usage over time, including information about which parts of the program are responsible for the most memory allocations. The graph is supplemented by a text or HTML file that includes more information for determining where the most memory is being allocated. Massif runs programs about 20x slower than normal.

Helgrind
Helgrind is a thread debugger which finds data races in multithreaded programs. It looks for memory locations which are accessed by more than one (POSIX p-)thread, but for which no consistently used (pthread_mutex_) lock can be found. Such locations are indicative of missing synchronisation between threads, and could cause hard-to-find timing-dependent problems. It is useful for any program that uses pthreads. It is a somewhat experimental tool, so your feedback is especially welcome here.

lothar
That looks wikid except it looks to be linux only. That was my bad should of added the extra info in.
Lodle
Typo in post. It's "valgrind" not "valdrind"
Gayan
@Gayan thanks for fixing it :-)
lothar
@Lodle If your program is portable the it should be easy to compile and run it on Linux to test it with Valgrind. Software that is as capable (or more) as Valgrind is unfortunaltly non-free on Windows.
lothar
A: 

I'm not sure of your requirement but I personally prefer Rational Purify. It's the best memory profiler I've come across.

Gayan
I really need something that can tell me where memory is allocated. Thanks for the link though
Lodle
+2  A: 

You can try this (all is free):
AMD CodeAnalyst Performance Analyzer for Windows®
Very Sleepy

lsalamon
Very Sleepy looks interesting. Anybody here have any experience with it? How good are the memory profiling features?
Naaff
A: 

Used Glowcode in the past with good results.

LK
Just tried it out, interface is a bit confusing and didnt really get what i wanted from it.
Lodle
I seem to remember using v5 because I also found the insterface from v6 to be non-trivial. The old version was very clear and easy to use.
LK
You can still find the old version around on the net
LK
+1  A: 

you could watch how much physical and disk memory your active programs are using ctrl-alt-delete. Enter pauses in the program and wait for command line inputs to determine memory at various points (debugger could work as well)

or spend money Not cheap but: http://www.compuware.com/products/devpartner/studio.htm

Mark Essel
That looks like what i need. Thanks
Lodle
I use DevPartner but I don't think this is what the author is looking for as DevPartner only has memory analysis for managed code, not native c++.
JProgrammer
That last comment is complete rubbish. I use Dev Partner 9 for native C++, and it works brilliantly.
Thomi
A: 

You might want to give goole-perftools a try. Heap profiling is included in TCMalloc, and I believe it is possible to "reset" heap usage just before your "certain stage", and query the heap state right after it.

Employed Russian
A: 

how about DebugDiag . It can give the snapshot of memory usage at different points of time.Plus its free.

Alien01
+2  A: 

Try Memory validator.

Canopus
+1  A: 

You might want to look at the CRT Debug Heap (via crtdbg.h) that is already available in VS2005. The reports are crude and there's no UI to drive it (unless you can find something on Google maybe), but it might have the information you're looking for - and you already have it:

Michael Burr
The good news about this approach is that it's very low-level, so you can achieve pretty much anything. You can add a "hook" function to each memory allocation, and the CRT will call that function back (in a thread-safe manner). The function can keep a count of allocations, log them, or whatever. Be aware that this will not keep track of allocations in the COM heap (CoCreateInstance or SysAllocString)
Drew Hoskins
A: 

I just tried "leakfinder" by Jochen Kalmbach, from codeproject, that someone mentioned in an answer for a similar question.

leakfinder

It is easy to use (two files to add to your project, and two functions to call from your main), and it actually works. It generates log files containing the sequence of calls that leads to leaks, and come with an analysis tool that displays the info coming from the log file in a GUI. It even points the actual source code that caused it.

I already found actual leaks in real-life code.

Jem
I dont want a leak finder. I want a memory profiler that can tell me where my memory is allocated.
Lodle
A: 

There is an article named "Design and Implementation of an In-Game Memory Profiler" in the book "Game Programming Gems 8" that provide an implementation of a memory allocation profiler on Windows. A preview of the article is avaliable online at http://my.safaribooksonline.com/9781584507024/ch37, and the source code should came along with the book's companion CD.

Note that the profile is not just able to find memory leaks, but it's able to show the memory usage in a hierarchical call-stack view. Moreover, a remote GUI viewer is avaliable for real-time monitoring with little effort.

Ricky Lung
A: 

try dmalloc. works good for c programs. http://dmalloc.com/

Virender Kashyap