title: How to profile the memory consumption by a set of C++ classes?
body: <p>I am trying to figure out the memory consumption by my (C++) program using gprof. The program does not have a gui, it is entirely cli based.</p>
  
  <p>Now, I am new to gprof, so I read a few tutorials, that taught me how to run gprof and spot <em>time</em> consumption.</p>
  
  <p>However, I need to find out the <em>memory</em> consumption by a specific set of classes.
  Say there is a program with many types, <code>A, ..., Z</code>. Now I want to run my program and see how many accumulated memory was used by objects of the classes <code>A, E, I, O, U</code> (for example).</p>
  
  <p>Have you guys any ideas or pointers how I could approach this task?<br>
  I am not exclusively considering gprof, I am open for any (fos) software that gets the job done.</p>
  
  <p>I have, of course, searched both google and stackoverflow.com for any answer to this problem, but either I use the wrong keywords or there is nobody having this problem out there.</p>
  
  <p>Edit: Suggestions about doing this manually are obvious. Of course I could code it into the application, but its about a great deal of classes I would rather not change. Also, I want to have the total memory consumption, so I cannot only count all created objects, because I would have to track the size of the object individually.</p>
  
  <p>Edit2: I went with a modification of <a href="https://stackoverflow.com/questions/3585341/how-to-profile-the-memory-consumption-by-a-set-of-c-classes/3585398#3585398">DeadMG's suggestion</a>, which I only have to inherit from. It works pretty well, so, if anybody has as similar problem, try this.</p>
  
  <pre><code>class GlobalObjectCounter {
    public: 
      struct ClassInfo {
        unsigned long created;
        unsigned long destroyed;
        unsigned short size;
        ClassInfo() : created(0), destroyed(0), size(0) {}
        ClassInfo(unsigned short _size) : created(0), destroyed(0), size(_size) {}
        void fmt(std::ostream&amp; os) {
          os &lt;&lt; "total: " &lt;&lt; (this-&gt;created) &lt;&lt; " obj = " &lt;&lt; (this-&gt;created*this-&gt;size) &lt;&lt; "B; ";
          os &lt;&lt; "current: " &lt;&lt; (this-&gt;created-this-&gt;destroyed) &lt;&lt; " obj = " &lt;&lt; ((this-&gt;created-this-&gt;destroyed) * this-&gt;size) &lt;&lt; "B; ";
        }
      };
    protected:
      static std::map&lt;std::string,ClassInfo&gt; classes;
      GlobalObjectCounter() {}
    public:
      static void dump(std::ostream&amp; os) {
        for (std::map&lt;std::string,ClassInfo&gt;::iterator i = classes.begin(); i != classes.end(); ++i) {
          os &lt;&lt; i-&gt;first &lt;&lt; ": ";
          i-&gt;second.fmt(os);
          os &lt;&lt; "\n";
        }
      }
  };
  
  template &lt;class T&gt; class ObjectCounter : public GlobalObjectCounter {
    private:
      static ClassInfo&amp; classInfo() {
        static ClassInfo&amp; classInfo = classes[std::string("") + typeid(T).name()];
        classInfo.size = sizeof(T);
        return classInfo;
      }
    public:
      ObjectCounter() {
        classInfo().created++;
      }
      ObjectCounter(ObjectCounter const&amp; oc) {
        classInfo().created++;
      }
      ObjectCounter&amp; operator=(const ObjectCounter&amp;) {}
      ~ObjectCounter() {
        classInfo().destroyed++;
      }
  };
  </code></pre>
  
  <p>The map lookup is a bit nasty, I admit it, but I didn't have the nerve to take care of storing the iterator for each class. The main issue was that you would have to explicitly initialise it for each counted class. If you know how to do that better, tell me how.</p>