Important: Please read the Qt Code of Conduct -

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.


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


    #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 )


    #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();


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


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

  • Lifetime Qt Champion


    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 ?

  • @SGaist does it work , even i have the same problem ?

  • Lifetime Qt Champion

    @Prasad_Intel Did you try what I suggested ?

Log in to reply