QML Loader leaks memory



  • Hi all,

    I am developing application using Qt/QML. I have used QML Loader to load the qml file.when i load the file using loader it occupies memory but weird thing is when i load and unload the file in loader, every time i load file it takes few memory and memory is continuous increasing. can any one has solution ?



  • Hi,

    In general Loader should be unloading the previous item when a new one is loaded. It does use deleteLater, which means e.g. changing the source file in a loop will show increasing memory usage until the event loop runs.

    What version of Qt are you using? How many different files are you loading (e.g. are you toggling between two different files, or loading x new files)? It's possible the component itself is being cached by the engine and not cleared, which means each time a new component was loaded for the first time it would show an increase that would persist.

    Regards,
    Michael



  • Hi Michael,

    I am using 35 different qml files and at a time only one or no one would be in loader. when i load the new qml file it increase the memory but strangly it is not releasing when i remove it from the loader.even after load-unload-load same file will also increase the memory but the different is, it occupies lesser memory than first time occupied. I am using qt 4.7.3 but i also checked with 4.7.4 and got same result.

    if engine is not clearing the cache then how can i clear it?



  • Hi,

    Do you mean that on unload no memory is released, or or that only some of the memory is released? I would expect the latter to be the case, with the component cache growing each time a completely new qml file is loaded (and this growth should at worst be bounded by the number of unique qml files that need to be loaded).

    You could try using QDeclarativeEngine::clearComponentCache() to see if that makes any difference in your case. There is also a manual gc() function you can call from JS to force garbage collection, though I wouldn't expect it to make a difference in this case.

    Regards,
    Michael



  • Hi,

    Yes exactly, Loader is not releasing memory when i specify source as null. but if now i assign new qml file (other than before assigned) it will allot memory from previously unfreed memory if possible. you can check by simply making Loader element which simply loads two different file which just contains lots of rectangles.



  • Have you been able to resolve this? I'm running into this exact same thing.



  • How serious is that memory leak ?



  • I've made similar observations. I suspect it's memory fragmentation.



  • Our leak is very serious, on a fully loaded QML (images and stuff) it leaks about 1MB per load. Which is really bad considering we have about 128MB total memory on the Windows CE device we use. We have to reboot the Windows CE device way to often. (several times a day, TPHB is not really happy about that, nor am I).



  • Hi altera,

    I havent sort out this problem but rather to reduce start up delay leakage in my application i have loaded files only once and afterwards setting visible to true/false so finally i am not unloading file.



  • Yes, that is what I tried as well but sadly due to the memory constraints that pushed us up right to the maximum. Which caused more out of memory errors.



  • Are you sure the memory leaks are not because you forgot to free memory you allocated on the heap ?



  • No I'm 100% sure on top of that I've recreated the leak using 2 empty QMLs

    https://bugreports.qt-project.org/browse/QTBUG-26032?focusedCommentId=179280#comment-179280



  • One thing that I did notice is that when I add

    @m_View->engine()->clearComponentCache();@

    The leak is 10 times worse!


  • Moderators

    This is all rather scary. Happens for me, too - due to big JPGs/PNGs loaded, it adds +40MB per unload.



  • Does it happen only on Qt 4.7.4 or also on Qt 4.7.3 Qt 4.8 and Qt 5 ?



  • 4.7.4 + 4.8.2 I have not tested 5 since we require Windows CE support.


  • Moderators

    In my case:

    4.7.4, 4.8.1 - QtQuick 1.
    Qt5.0.0 - QtQuick 2.



  • I also saw it on embedded linux.
    Its a very serious leak.
    It happens all the time there is a loading of new qml pages



  • We found that the reason for the memory leak is Qt's function:

    QDeclarativeEngine::clearComponentCache()



  • Please report a bug in the bug tracker then. Not many Qt Developers (those building Qt itself, not so much those using it) read these forums.



  • Can you please send me a link ?



  • The "bugtracker":https://bugreports.qt-project.org we usually call "Jira" (after the name of the system it is using). If you report a bug, please:

    Put a link to this forum topic in the bugreport, and

    Put a link to the bugreport in this forum topic.



  • somebody has resolved this issue?



  • Firstly, why is anyone calling clearComponentCache() in QtQuick 1? You have to be extremely careful about the circumstances in which you do so, or all sorts of craziness can occur.

    In QtQuick2, I seem to recall that we added / changed the functionality significantly (and updated the documentation) to clearly reflect what those functions should be used for and how.

    It's been a while, though, and my memory isn't too great. If someone can link the JIRA task I can take a brief look, but I can't promise to spend too much time on it, unfortunately.

    Cheers,
    Chris.



  • I'm not sure if there are leaks.

    I added to my qml pages:
    @
    Component.onDestruction:{
    componentX.destroy()
    componentY.destroy()
    componentZ.destroy()
    }
    @
    for every nested component

    and I'm using http://www.sikuli.org/ to test the complete flow (6 pages with many images). I had used my app 2000 times and there is not delay or crash.

    Hope this helps



  • Instead of calling app.exec() exec your event loop like this and see if it still leaks.

    {
    app.processEvents(..);
    view->engine()->collectGarbage();
    view->engine()->clearComponentCache();
    }



  • We have had a lot of problems with unexplained memory loss running 4.8.2 on Wince 6.0. Using 4.8.5 reduced the problem significantly.

    As we have lots of Loaders in qml we also started to call gc() explicity for every onSourceChanged: call. The gc() was hardly ever called, it seemed. I've heard that this has improved in Qt5, but we're not in a position to test this now.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.