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. QML: memory problem
Qt 6.11 is out! See what's new in the release blog

QML: memory problem

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 639 Views 1 Watching
  • 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.
  • N Offline
    N Offline
    Nunnito
    wrote on last edited by
    #1

    Hello!
    I am very new to QML, sorry if my question is a bit silly.

    I would like to know the reason why when I destroy an item that takes a lot of memory, this item does not free the memory.

    Example:

    import QtQuick 2.15
    import QtQuick.Controls 2.15
    
    ApplicationWindow {
        visible: true; width: 750; height: 750
        Button {
            y: 250
            text: "More"
            onClicked: repeater.model += 5000
        }
        Button {
            y: 500
            text: "Less"
            onClicked: repeater.destroy()
        }
        Row{
            Repeater {
                id: repeater
                model: 0
                Button {}
            }
        }
    }
    

    In the code, when I press the "More" button, about 250MB is stored in memory.
    And when I press the "Less" button, the repeater is destroyed, but QML does not release the memory that the repeater was taking.

    I would like to know the reason for this.
    Thanks for your time!

    ODБOïO 1 Reply Last reply
    0
    • N Nunnito

      Hello!
      I am very new to QML, sorry if my question is a bit silly.

      I would like to know the reason why when I destroy an item that takes a lot of memory, this item does not free the memory.

      Example:

      import QtQuick 2.15
      import QtQuick.Controls 2.15
      
      ApplicationWindow {
          visible: true; width: 750; height: 750
          Button {
              y: 250
              text: "More"
              onClicked: repeater.model += 5000
          }
          Button {
              y: 500
              text: "Less"
              onClicked: repeater.destroy()
          }
          Row{
              Repeater {
                  id: repeater
                  model: 0
                  Button {}
              }
          }
      }
      

      In the code, when I press the "More" button, about 250MB is stored in memory.
      And when I press the "Less" button, the repeater is destroyed, but QML does not release the memory that the repeater was taking.

      I would like to know the reason for this.
      Thanks for your time!

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      @Nunnito hi and welcome
      Please read the warnings here : https://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html#deleting-objects-dynamically

      1 Reply Last reply
      0
      • N Offline
        N Offline
        Nunnito
        wrote on last edited by
        #3

        Hi, thanks for the answer.

        I created the following code using Qt.createComponent():

        import QtQuick 2.15
        import QtQuick.Controls 2.15
        
        ApplicationWindow {
            visible: true; width: 250; height: 250
        
            Button {
                y: 125
                text: "Destroy"
                onClicked: {
                    for (var i = 0; i < row.children.length; i++) {
                        row.children[i].destroy()
                    }
                }
            }
        
            Row {
                id: row
            }
        
            Component.onCompleted: {
                var component = Qt.createComponent("MakeButton.qml")
        
                for (var i = 0; i < 5000; i++){
                    component.createObject(row)
                }
            }
        }
        

        And the problem persists, the memory is not freed.

        Is something wrong with the code?

        ODБOïO 1 Reply Last reply
        0
        • N Nunnito

          Hi, thanks for the answer.

          I created the following code using Qt.createComponent():

          import QtQuick 2.15
          import QtQuick.Controls 2.15
          
          ApplicationWindow {
              visible: true; width: 250; height: 250
          
              Button {
                  y: 125
                  text: "Destroy"
                  onClicked: {
                      for (var i = 0; i < row.children.length; i++) {
                          row.children[i].destroy()
                      }
                  }
              }
          
              Row {
                  id: row
              }
          
              Component.onCompleted: {
                  var component = Qt.createComponent("MakeButton.qml")
          
                  for (var i = 0; i < 5000; i++){
                      component.createObject(row)
                  }
              }
          }
          

          And the problem persists, the memory is not freed.

          Is something wrong with the code?

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by ODБOï
          #4

          @Nunnito apparently QML keeps the memory for future usage as you can reed here or here
          you can test it my creating and deleting objects several times, you will see the memory consumption will stay stable

              Row{
                  Button {
                      y: 125
                      text: "Create"
                      onClicked: {
                          var component = Qt.createComponent("MakeButton.qml")
                          for (var i = 0; i < 2000; i++){
                              component.createObject(row)
                          }
                      }
                  }
          
                  Button {
                      y: 125
                      text: "Destroy"
                      onClicked: {
                          for (var i = 0; i < row.children.length; i++) {
                              row.children[i].destroy()
                          }
                      }
                  }
              }
          
              Row {
                  id: row
              }
          

          https://blog.basyskom.com/qtws15-a-deep-dive-into-qml-memory-management-internals/ this is a bit old thought but still very interesting

          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