Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML: memory problem



  • 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!





  • 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?



  • @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


Log in to reply