Important: Please read the Qt Code of Conduct -

Memory use with repeated creation/destruction of ListView's

  • Hi, everyone.

    Qt 4.7.3, Windows
    Qt 4.7.2, Linux fedora

    I have a problem with a program we are currently developing here. We repeatedly create and destroy ListView's and it seems that memory is leaking.

    Our code is in C++ (with QDeclarativeComponent::create), but one of my co-workers wrote a QML-only version that exhibits the problem in QMLViewer. I attached the two QML files to the present message. Load the memory_leak.qml file in QMLViewer and press Return repeatedly (or hold it down) to create ListView's from TemplateListView.qml.

    If you check the memory used by the QMLViewer process, you will see it increase in time. You can add pictures in the model/view to make it more obvious.

    What are we doing wrong?

    2 Files below:
    import QtQuick 1.0

    id: root

    function createList(){
        var qtComponent = Qt.createComponent("TemplateListView.qml");
        var qtObject = qtComponent.createObject(container);
        if (qtObject == null){
            console.log("Error in createList() : qtObject is null !");
        container.isEmpty = false;
    width: 500
    height: 600
    focus: true
    Keys.onReturnPressed: if (container.isEmpty) createList()
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.topMargin: 40
        width: 500
        height: 40
        text: "press Return to create a listview and the listview will auto destructs after 100 ms."
        id: container
        property bool isEmpty: true
        anchors.topMargin: 100
        width: 500
        height: 500
        id: myModel
        ListElement{ label: "label1" }
        ListElement{ label: "label2" }
        ListElement{ label: "label3" }
        ListElement{ label: "label4" }
        ListElement{ label: "label5" }
        ListElement{ label: "label6" }
        ListElement{ label: "label7" }
        ListElement{ label: "label8" }
        ListElement{ label: "label9" }
        ListElement{ label: "label10" }


    import QtQuick 1.0

    id: template

    anchors.fill: parent
    opacity: 1
    focus: false
    model: myModel
    delegate: Component{
            width: template.width
            height: 100
            color: "grey"
            Text{ text: label }
    highlight: Rectangle { color: "black" }
    Component.onCompleted: opacity = 0;
    Behavior on opacity {
        NumberAnimation {
            duration: 100
            onRunningChanged: if (!running) {template.parent.isEmpty = true;template.destroy();}



  • I've had some similar concerns about Loaders. In my code I'm constantly changing screens and using Loaders to create/destroy new/current screens. When I look at my memory manager (top and task manager) all I see is a climbing number. It never seems to go back down.

    I think behind the scenes model-based QML elements also load visual elements dynamically.

    I use visual leak detector on the Windows side and it reports no leaks. Is the cause a lazy garbage collection algorithm?

  • I've had similar problem about Loader and ListView, when I show and hide Page, contain Loaders with ListView, memory increase incremently, and not go down :(.

    Could you help me ?

    Many thanks

  • Hi,

    A couple notes:


Log in to reply