Increasing memory consumption with creating and destroying qml windows



  • I'm fairly new to QtQuick and have noticed a weird issue when creating and destroying windows. We have a large application that has many different complex windows, and I've noticed that when creating them they use a large amount of memory(several mbs) and when I destroy them memory consumption doesn't seem to go down at all. I just want to make sure I'm closing them correctly or if there is something else I should be doing instead. Maybe re-using them? Just looking for someone who is more experienced with QtQuick to give an explanation.

    I've found a few posts from other people experiencing the same issue, but every answer seems to say that it stops eventually and I have yet to see this. We've used Sikuli in our application to rapidly open and close some of our windows and eventually it will use so much memory that it crashes the system.

    I created a mini test app that demonstrates the issue I am experiencing. I'm not sure if this is a memory leak or just Qt allocating a large amount of memory up front to use, but it seems like the consumption never stops. I ran the below code and it eventually climbed up to 2 gigs of memory. I was just using task manager to monitor the memory usage, not any other tools.

    ObjectCreator.h

    #pragma once
    
    #include <QQmlApplicationEngine>
    #include <QObject>
    
    class ObjectCreator : public QObject
    {
       public:
          explicit ObjectCreator( QQmlApplicationEngine* engine );
    
       public slots:
          void createAndDestroyWindow() const;
    
       private:
          Q_OBJECT
    
          QQmlApplicationEngine* engine;
    };
    

    ObjectCreator.cpp

    #include <QQmlComponent>
    #include <QWindow>
    
    #include "ObjectCreator.h"
    
    ObjectCreator::ObjectCreator( QQmlApplicationEngine* engine ) :
       engine{ engine }
    {
    }
    
    void ObjectCreator::createAndDestroyWindow() const
    {
       QWindow* window = nullptr;
    
       QQmlComponent component( engine, QUrl( QString( "qrc:///Background.qml" ) ) );
       if ( component.status() == QQmlComponent::Ready )
          window = qobject_cast< QWindow* >( component.create() );
    
       if ( window != nullptr )
       {
          window->close();
          window->destroy();
       }
    }
    

    Main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    
    #include "ObjectCreator.h"
    
    int main( int argc, char* argv[] )
    {
       QGuiApplication app( argc, argv );
       QQmlApplicationEngine engine;
    
       auto objectCreator = new ObjectCreator( &engine );
       engine.rootContext()->setContextProperty( "objectCreator", objectCreator );
       engine.load( QUrl( QStringLiteral( "qrc:/main.qml" ) ) );
    
       return app.exec();
    }
    

    Main.qml

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    
    ApplicationWindow
    {
       id: mainWindow
    
       visible: true
       width: 200
       height: 200
    
       Timer
       {
          interval: 50
          running: true
          repeat: true
    
          onTriggered: objectCreator.createAndDestroyWindow();
       }
    }
    

    Background.qml

    import QtQuick 2.7
    import QtQuick.Window 2.3
    
    Window
    {
       id: backgroundWindowContainer
    
       height: Screen.height
       width: Screen.width
    
       Rectangle
       {
          id: backgroundRect
    
          anchors.fill: parent
       }
    }
    

  • Lifetime Qt Champion

    Hi,

    From a quick look at QWindow's documentation, destroy releases the native platform resources, not the whole object.

    What about calling delete or rather deleteLater on the object you created ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.