Important: Please read the Qt Code of Conduct -

Using C++ Object to test QML Garbage Collection

  • In my mobile game , I use QML. The performance is not stable and I am wondering whether the reason is garbage collection.
    Since I haven't found way to know when the QML does Garbage Collection, I used such way:

    I register a C++ type into QML , and create an instance in my JavaScript code. The C++ type is different in two points:

    (1)In constructor, QQmlEngine::setObjectOwnership(this,QQmlEngine::JavaScriptOwnership);
    (2)In destructor, emit gcSignal();

    So, eveytime QML does garbage collection, the instance will be destructed, and I can receive the gcSignal().

    Is this a useful way?
    Is there any other way to help?

  • That's one way of doing it. The problem for you would be to connect every object to make to a listeren of your gc signal.

    Before you go deeper into garbage collection, have you profiled the application with the qml profiler?

  • The phenomenon is that my game lags periodically. It is not obvious , but I can feel it. So I doubt it is of gc reason.

    I just use one object,I call it "ghost", to listen of the gc signal, and everytime gc happens, the ghost will destructed, and a new ghost will be created.

    If I thought it were right,I could receive the signal. And it works as I thought. What is weird is that I can not receive the signal if I add some irrelated code such as just add one line:var x = new Date(),when re-create the ghost. So confused.

    Yes , I profiled the application before.

Log in to reply