tags:

views:

421

answers:

4

I'm using Qt and C++, I need to find out the amount of memory used by instances of certain Qt classes, this is usually done using sizeof, however in Qt each class holds a pointer to an another class containing the actual implementation, the definition of this private implementation class is not found in headers but only in the source-code.

I'm new to Qt so may be there's a standard way to do this, if there isn't do you have any ideas to solve this?

+9  A: 

There is no way of doing this in Standard C++, and very few frameworks support anything like it. The reason is fairly simple - as you have observed, an object may contain pointers, and those pointers may point to further objects that contain pointers, and so on. And even when you get to the end of the pointer chain, there is no general method to find out how much memory a pointer points to.

So, you need to find another way to solve your problem, which I don't believe is the right problem to solve in the first place.

anon
+4  A: 

There's no exact answer to the question, since the amount of memory allocated for different objects of the same type might not even be the same (e.g. QSomething A might be able to reuse some data from a cache whereas QSomething B might have to allocate it separately, etc).

What you could do, I suppose, is write a trivial test program that starts up, allocates N of the object in question, then goes to sleep() for a long time. While the program is sleeping, use Task Manager (or whatever tool you prefer) to see how much RAM the process is using. Then ctrl-C (or kill) the process, and run it again with a larger value for N, and repeat the measurement. Repeat that process and eventually you'll get an idea of how the process's RAM allocation grows with the number of items allocated, and then you can do a little algebra to get a ballpark idea of the average memory cost per object.

(keep in mind that there's a good bit of memory overhead just in starting the process, so subtract the memory used by the N=0 case from all the cases so that you're measuring just the objects' costs and not the environmental overhead)

Jeremy Friesner
+3  A: 

The problem isn't unique to Qt, in fact. Consider how much space a std::string uses. It's neither sizeof(std::string) nor std::string::size().

C++ doesn't have an answer to this question because the question rarely makes sense.

MSalters
For std::string, using sizeof(std::string) + string length gives you an almost correct number.One should have an indication of how heavy an object is to be able to write efficient applications.I find it strange that such a question rarely makes sense in a language that allows low-level programming and gives you absolute control.
Diaa Sami
To name just a few reasons it's not that reliable : Small String Optimization, size rounding, heap overhead, COW. Wrt your second remark, I haven't encountered a situation yet in which the lack of this information hindered me.
MSalters
@Diaa It does give you absolute control, but if you want that, you can't use libraries like Qt or indeed the Standard Library - you have to write your own. But the fact that none of these libraries provide the feature your question asks about should suggest to you that it is not a particularly useful feature to implement.
anon
@Neil, @MSalters thanks for your responses, I understand your point of view but I'm not convinced. I'd like to discuss this more but the comments are not very suitable for this.
Diaa Sami
+1  A: 

I've found that in general QObject is rather heavy for creating more than a few thousand instances.

As several other people have mentioned, often the best way to work this out is to try it and see what happens. You could even write a small application like this:

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QObject objects[5000];
    return app.exec();
}

Then measure the memory usage before you exit the application.

Thomi