amit kumar
amit kumar

Reputation: 21042

Memory Allocation Profiling in C++

I am writing an application and am surprised to see its total memory usage is already too high. I want to profile the dynamic memory usage of my application: How many objects of each kind are there in the heap, and which functions created these objects? Also, how much memory is used by each of the object?

Is there a simple way to do this? I am working on both linux and windows, so tools of any of the platforms would suffice.

NOTE: I am not concerned with memory leaks here.

Upvotes: 54

Views: 52941

Answers (12)

abyss.7
abyss.7

Reputation: 14462

Try the gperftools - it can:

  • profile specific parts of code or the whole program at once.
  • represent data via visual directed graphs showing exact function calls and their inheritance.
  • focus on a specific regions of code in a visual graph.
  • show diff between dumps.
  • show allocated space instead of used - all of this is done with the same dump.

Also, it almost doesn't affect the program efficiency.

Upvotes: 4

mtosic
mtosic

Reputation: 59

MTuner - a free C/C++ memory profiler. Description below:

MTuner is a multi platform memory profiling, leak detection and analysis tool supporting MSVC, GCC and Clang compilers. Features include: timeline based history of memory activity, powerful filtering, SDK for manual instrumentation with full source code, continuous integration support through command line usage, memory leak detection and much more. Profile any software targetting platforms with GCC or Clang cross compilers. Comes with built in support for Windows, PlayStation 4 and PlayStation 3 platforms and an platform targeted by a Windows based cross compiler.

Upvotes: 5

javier-sanz
javier-sanz

Reputation: 2524

Have you tried Valgrind? It is a profiling tool for Linux. It has a memory checker (for memory leaks and other memory problems) called Memcheck but it has also a heap profiler named Massif.

Upvotes: 25

Stewart Lynch
Stewart Lynch

Reputation: 129

I've just released a win32 native memory profiler MemPro, as a free beta. http://www.puredevsoftware.com/MemPro.htm. It hooks into new/delete and sends data to an external app where you can view the allocations in various different ways. Hope this is of help.

Upvotes: 2

sth
sth

Reputation: 229814

For simple statistics, just to find out where all the memory is used, you could add a template like this:

template<class T>
class Stats {
  static int instance_count;
public:
  Stats() {
    instance_count++;
  }
  ~Stats() {
    instance_count--;
  }
  static void print() {
    std::cout << instance_count << " instances of " << typeid(T).name() <<
        ", " << sizeof(T) << " bytes each." << std::endl;
  }
};

template<class T>
int Stats<T>::instance_count = 0;

Then you can add this as a base class to the classes you suspect to have a lot of instances, and print out statistics of the current memory usage:

class A : Stats<A> {
};

void print_stats() {
  Stats<A>::print();
  Stats<B>::print();
  ...
}

This doesn't show you in which functions the objects were allocated and doesn't give too many details, but it might me enough to locate where memory is wasted.

Upvotes: 12

Ricky Lung
Ricky Lung

Reputation: 680

Chapter 4.6 from Game Programming Gems Volume 8 (Safari Book preview link) details a advanced memory profiler by Ricky Lung which can show the allocation statistic in a hierarchical call-stack manner and yet support multi-threading.

Upvotes: 1

Nitin Bhide
Nitin Bhide

Reputation: 1695

For windows check the functions in "crtdbg.h". crtdbg.h contains the debug version of memory allocation functions. It also contains function for detecting memory leaks, corruptions, checking the validity of heap pointers, etc.

I think following functions will be useful for you.

_CrtMemDumpStatistics _CrtMemDumpAllObjectsSince

Following MSDN link lists the Heap State Reporting functions and sample code http://msdn.microsoft.com/en-us/library/wc28wkas(VS.80).aspx

Upvotes: 9

Michael Burr
Michael Burr

Reputation: 340406

Chapter 1.10 from Game Programming Gems Volume 2 (Amazon link) details a simple but effective Drop-in Debug Memory Manager by Peter Dalton that provides a decent set of statistics when you dump the log.

Upvotes: 1

amit kumar
amit kumar

Reputation: 21042

Just saw on AQtime site that they have a good support for "Allocation Profiling".

Upvotes: 0

Canopus
Canopus

Reputation: 7457

You may try Memory Validator from http://www.softwareverify.com/cpp/memory/index.html

It is one of the best tools I have come across for profiling memory usage. 30 days evaluation version is available for free download.

Upvotes: 6

Charlie Martin
Charlie Martin

Reputation: 112404

There are several things you can do. The simplest thing is to link a debug malloc library; there are aa number of them available, depending on the details of your environment (eg, google for _malloc_dbg for Windows.)

The second choice is that you can overload new and delete in C++; it's possible to overload the basic new and delete with new functions that track memory allocation and usage.

Upvotes: 3

Andy White
Andy White

Reputation: 88455

In a previous job we used a tool called "Purify." It's a product made by Rational/IBM. I don't think this is a free tool, but I remember it being pretty good. Here's some info:

http://en.wikipedia.org/wiki/IBM_Rational_Purify

Upvotes: 1

Related Questions