Memory Allocation Profiling in C++

40,132

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 5

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.

Share:
40,132

Related videos on Youtube

amit kumar
Author by

amit kumar

Python. AI. Learning Rust. Recovering C++ developer.

Updated on March 09, 2021

Comments

  • amit kumar
    amit kumar about 3 years

    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.

  • Nathaniel Sharp
    Nathaniel Sharp about 15 years
    There's even an Eclipse integration for Valgrind (wiki.eclipse.org/index.php/Linux_Distributions_Project)
  • javier-sanz
    javier-sanz about 15 years
    Nice tip, I didn't know that project. I normally use Cmake so with that tool and this: vtk.org/Wiki/Eclipse_CDT4_Generator I can move all my C++ developments to Eclipse CDT in Linux, thanks.
  • cib
    cib almost 11 years
    Is there a way to profile object count and total memory used per object type using valgrind massif?
  • Mathieu JVL
    Mathieu JVL almost 11 years
    Purify tells you about memory corruptions and leaks, not the size of allocated objects
  • Alastair Maw
    Alastair Maw over 10 years
    If you do this, do bear in mind this isn't thread-safe. You may way to use std::atomic (if you have C++11) or boost::atomic for the increment/decrements.
  • TankorSmash
    TankorSmash about 10 years
  • CoffeDeveloper
    CoffeDeveloper over 8 years
    the chapter is not immediatly visible, do you have the link for the real preview without forced subscribe buttons or free trials?
  • Amir Gonnen
    Amir Gonnen almost 7 years
    How do I file a bug report? I've tried MTuner and it crashes when launching an executable with a long command line. Is there a github repo?
  • Amir Gonnen
    Amir Gonnen almost 7 years
    It also crashes when trying to load a large .MTuner file (~9Gb). Do you try to load the whole file to memory all at once? Do you check for memory allocation failures on your code?
  • mtosic
    mtosic almost 7 years
    There's no github repo, it's closed source. Feel free to send more info to me about those crashes and I'll fix them/take a look.
  • Amir Gonnen
    Amir Gonnen almost 7 years
    How do I contact you?
  • mystery_doctor
    mystery_doctor over 3 years
    UPDATE : the new link for gperftools is now: github.com/gperftools/gperftools
  • Hi-Angel
    Hi-Angel about 2 years
    Does it support Linux though?
  • Hi-Angel
    Hi-Angel about 2 years
    The link is broken