Identifying which class takes the more memory in an application

D

david.jobet

Hello,

I'm trying to decrease the memory consumption of a large c++
application.
I'd like to identify the object which at a given t=T has the greatest
sizeof(object) * number_of_current_instance(object)

Do you know of any tools which can analyse that for me ?
I already made a search via google and found a lot of "memory leak
analysis" tools but no "allocation analysis" tools.

Alternative question : do you know of any mean to do it from the
inside without having to modify each constructor/destructor ?
I was thinking of overloading the global operators new and delete to
"count" live objects.
In the meantime, I thought I also need to have access to rtti to
identify the runtime type of the object but I'm pretty sure rtti of
the object is initialized *after* operator new is called, so I'm
pretty stuck.

Any idea ?

Thanks and kind regards

David
 
P

Puppet_Sock

I'm trying to decrease the memory consumption of a large c++
application.
I'd like to identify the object which at a given t=T has the greatest
sizeof(object) * number_of_current_instance(object)

Do you know of any tools which can analyse that for me ?
I already made a search via google and found a lot of "memory leak
analysis" tools but no "allocation analysis" tools.

Alternative question : do you know of any mean to do it from the
inside without having to modify each constructor/destructor ?
I was thinking of overloading the global operators new and delete to
"count" live objects.
In the meantime, I thought I also need to have access to rtti to
identify the runtime type of the object but I'm pretty sure rtti of
the object is initialized *after* operator new is called, so I'm
pretty stuck.

Any idea ?

Presumably you don't need to know this data for more than
a few classes. So, modifying their ctors and dtors should
not be that big of a deal. This won't get instances made
through implied ctors that the compiler invents for you,
so you need to be sure you've implemented ctors in every
case that will come up.

You could have a global object used for counting stuff.
Then include a macro in each ctor/dtor that you are
interested in keeping track of. In the ctor, the macro
calls the global object and says "plus one <mytype>".
The global object can do what ever level of logging is
required. Maybe you need the time, or maybe just the
sequence of calling. Then the macro in the dtor calls
the global object and says "minus one <mytype>".

So, the ctor for MyClass would simply get one more
line that looked like

COUNTPLUSONE("MyClass")

or some such. The hard part will be deciding what
info you need stored, how to store it, and what
reporting is then useful for you. Possibly you
want a data holder class for this, plus a vector
of that type.

You could easily have a compiler flag that turns all
of this off. Say, the macro gets converted to empty
blank lines with the flag set to OFF or some such.
Socks
 
D

david.jobet

You're assuming that I *know* which classes need tracking, and the
truth is I just don't know.

That's why I want to make a global pass on all objects to identify the
main consumers. Once this is done I will be able to start local code
modification.
The thing is, even if I have an idea of the biggest greedy object, I
don't want to rely on a feeling. I first want to run several tests
scenarios and have a global, impartial tool telling me which object is
*really* the big consumer.
 
J

Jacek Dziedzic

Hello,

I'm trying to decrease the memory consumption of a large c++
application.
I'd like to identify the object which at a given t=T has the greatest
sizeof(object) * number_of_current_instance(object)

What about classes that allocate memory for themselves?

class foo() {
double* private_stuff;
public:
foo() { private_stuff = new double[10000000]; }
// etc.
};

sizeof(new foo()); // rahter meaningless
 
D

david.jobet

Yes, that's a problem but there are not too much of them.
This pattern seems to be used only in streams/marshallers/cache/GUI in
the application I'm looking into.
My concern is more on business objects which to my knowledge don't do
that.
 
P

Puppet_Sock

You're assuming that I *know* which classes need tracking, and the
truth is I just don't know.
[snip]

If your app is so large and complicated that you can't
just say which ones are the possible pigs, then you
really ought to have already purchased some CASE tools.
Socks
 
D

david.jobet

I found tools for linux, but no tools for windows, but I was able to
code a little tool that does the trick.
Basically, I define a global new and global delete allowing me to keep
track of all allocations.
I launch a thread that at a given frequency scan all registered
allocated memory block using rtti to get the class name of the
allocated memory block, sort it and dump it to a file.

I had to put several tricks in play to do it (virtual table checking
against process address space to avoid crash, using a virtual class so
that rtti works, and checking the result of typeid).
Several links to help :
- win32 heap start/stop :http://www.ddj.com/184416272
- vt verification code, IUnknown trick and tpe verification code :
http://lxr.mozilla.org/mozilla/source/tools/trace-malloc/lib/nsTypeInfo.cpp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top