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. Memory use with repeated creation/destruction of ListView's
Forum Updated to NodeBB v4.3 + New Features

Memory use with repeated creation/destruction of ListView's

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 6 Posters 4.5k 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.
  • S Offline
    S Offline
    SergeQt
    wrote on last edited by
    #1

    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:
    main.qml
    @
    import QtQuick 1.0

    Item{
    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 !");
            return;
        }
        container.isEmpty = false;
    }
    
    width: 500
    height: 600
    focus: true
    
    Keys.onReturnPressed: if (container.isEmpty) createList()
    
    Text{
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: parent.top
        anchors.topMargin: 40
        width: 500
        height: 40
        text: "press Return to create a listview and the listview will auto destructs after 100 ms."
    }
    
    Item{
        id: container
    
        property bool isEmpty: true
    
        anchors.top: parent.top
        anchors.topMargin: 100
        width: 500
        height: 500
    }
    
    ListModel{
        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" }
    }
    

    }
    @

    TemplateListView.qml
    @
    import QtQuick 1.0

    ListView{
    id: template

    anchors.fill: parent
    opacity: 1
    focus: false
    model: myModel
    delegate: Component{
        Rectangle{
            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();}
        }
    }
    

    }
    @

    --
    Serge

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hornsj2
      wrote on last edited by
      #2

      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?

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

        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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbrasser
          wrote on last edited by
          #4

          Hi,

          A couple notes:

          • Because of the way QML works, it is normal to see memory usage increase up to a point. This is due to things like image caches and a garbage collected JS environment. There is a gc() function you can call from QML to manually trigger a garbage collection, if you'd like to see if it helps.
          • From memory there were a number of memory leaks fixed post 4.7.3, such as http://qt.gitorious.org/qt/qt/commit/3750d24175904916fe5ab8b1a76db956b1904e72 and http://qt.gitorious.org/qt/qt/commit/340c22999d9f9678c9035c1dc372423e970b8ca7. I'm not sure if these make any difference in your particular cases, but if you are able you could also try the 4.8 beta and see if things are improved there.

          Regards,
          Michael

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Choudhary Ramesh
            wrote on last edited by Choudhary Ramesh
            #5

            Still facing this same problem in qt 6.2 . when i push and remove page in stack view ,program memory kept rising and never go down even after manually call to gc();
            The page contain toolbar,image ,rectangle and buttons and doesn't contain any custom qml type .

            1 Reply Last reply
            0
            • H Hornsj2

              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?

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #6

              @Hornsj2 you may need to make a debug build of Qt in order to see the leaks of Qt code. Often people install Qt release build and will not be able to see the leaks of Qt.

              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