Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Increasing memory consumption with creating and destroying qml windows
Forum Update on Monday, May 27th 2025

Increasing memory consumption with creating and destroying qml windows

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 897 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eventh
    wrote on last edited by
    #1

    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
       }
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply
      0
      • SGaistS SGaist

        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 ?

        P Offline
        P Offline
        Prasad_Intel
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Prasad_Intel Did you try what I suggested ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved