4

Instantiating a QObject derived class on the heap in C++ gives me roughly about 160 bytes per object.

Registering the same object via qmlRegisterType() for creation from QML and creating the object dynamically via createObject() gives me a little over 2000 bytes per object.

That's quite the difference, almost 13 times more memory per object is being used when creating from QML. Note that the object doesn't have any bindings or anything extra compared to how it is in C++.

Naturally, being QObject derived means it is a non-visible object. So it shouldn't have any extra overhead as far as scene-graph rendering is concerned.

Note that I have ran tests with a significant amount of objects multiple times, getting consistent results and scaling up proportionally, so it is unlikely that it is due to some variance or QML engine memory pre-allocation.

Why is that happening? Is it normal? Is there a way to reduce it?

dtech
  • 47,916
  • 17
  • 112
  • 190
  • 1
    One thing you could look into is if there is a dynamically created QMetaObject. I.e. QML allows to add properties to object "instances", basically resulting in an ad-hoc "subclass" with a meta object that has these properties. Maybe one of those is created for every registered type. Still weird that it would be so big – Kevin Krammer Jan 04 '17 at 15:36
  • @KevinKrammer the meta object should be one per type, no? The test in question instantiated one single type, and all instances of it should refer to that shared meta type... The overhead lies elsewhere, possibly the qml revamp for qt 6 will improve the situation. So far, despite the many claims of improving memory efficiency, qml has largely remained a bloated memory hog. – dtech Aug 01 '21 at 07:05

0 Answers0