ulak blade
ulak blade

Reputation: 2665

Qt heap fragmentation - can QObjects be placed in a memory pool?

I have a Qt app where a large amount of QObjects in a list is frequently allocated and deleted as the list is updated.

With time the heap becomes heavily fragmented and the memory usage grows.

I would usually make an object pool where they are allocated contiguously in a large chunk, however this isn't possible with QObject.

Each QObject must be allocated separately which is a problem for performance. What's even worse is that there is an object hierarchy, so parents delete their children and I have no access to the delete calls, so I can't just write MyPool->Free(obj);

Unless I overload the new and delete operators?

Upvotes: 4

Views: 1062

Answers (2)

Swift - Friday Pie
Swift - Friday Pie

Reputation: 14673

Pretty much, if you have case, where you need use pool architecture, you shouldn't use Qt's hierarchy. Use appropriate, effective containers. If it's case where you need that to manage Qt object, mate your pool and structures you allocate in it with preallocated pool of objects you need work with. To add insult to injury in some cases QT copies objects into insides of their containers without our knowledge, while in other cases it only stores pointers. I'd say, Qt isn't a memory management-friendly framework.

Upvotes: 0

peppe
peppe

Reputation: 22816

It's not going to help much. To preserve binary compatibility, QObject uses the PIMPL idiom. A QObject is pretty much like this:

class QObject {
    QObjectData *d;
};

And sizeof(QObject) == sizeof(void *). The actual allocation of the d-pointer has a "sensible" size (and will trigger further allocations) and that's almost totally out of your control unless you're willing to do massive hacks...

Upvotes: 1

Related Questions