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

QStringList on Linux seems the keep memory



  • Hi all,

    I'm using Mint Linux 19 based on Ubunut 18.04 with Qt 5.9.7 and Qt 12.0.

    I have a problem using QStringList with many, many elements.
    When I start my app, it uses about 40 MB of main memory.
    When I create the list, the main memory is increased by 300 MB.

    for( int i = 0; i < 5000000; i++ )
    {
        m_list.append( QString::number( i ) );
    }
    

    When I clear the list either with

    m_list.clear();
    

    or with (I know this is bad, it's for testing purposes)

    while( m_list.size() > 0 )
    {
        m_list.removeFirst();
    }
    

    most of the allocated memory remains.
    First, I thought about a bug, but when I recreated the list the memory did not grow any further. And this is no problem on Windows.

    A colleague of mine said that this is an absolutely normal behaviour on linux. Is seems to be a caching problem.

    Can somebody confirm this?

    regards
    Oliver


  • Qt Champions 2017

    @stvokr said in QStringList on Linux seems the keep memory:

    m_list.removeFirst()

    You are only clearing the list. You are not deleting the QString objects. This could be the issue.



  • Unfortunately not. The memory is not growing when I recreate and delete the complete list over and over again.

    I have also tested it using MyClass and QString Pointers within the list.

    // create
    m_list = new QList< MyClass* >(); // new QList< QString* >();
    
    ...
    
    // clear
    while( m_list->size() > 0 )
    {
        MyClass* m = m_list->takeFirst();
        delete m;
        m = nullptr;
    }
    m_list->clear();
    delete m_list;
    m_list = nullptr;
    

    And it's the same behaviour such as QStringList.


  • Moderators

    The operating system is managing the memory for you. When your application requests (say) 300MB of RAM, then clears it - OS will keep it assigned to the process until another application requests that memory. This is done to optimise memory usage (next time your app wants some memory, system does not need to look for a free chunk - it's already available).


  • Moderators

    @stvokr said in QStringList on Linux seems the keep memory:

    A colleague of mine said that this is an absolutely normal behaviour on linux.

    Yes, that's right. Memory reports on Linux can be tricky to interpret (see e.g. https://stackoverflow.com/questions/131303/how-to-measure-actual-memory-usage-of-an-application-or-process )

    How are you checking memory usage?

    @dheerendra said in QStringList on Linux seems the keep memory:

    You are only clearing the list. You are not deleting the QString objects. This could be the issue.

    QString does not need to be manually deleted, especially since the OP did not use new to create them.

    Anyway, QString, QList, etc. are implicitly shared and should not be constructed using new. They manage their own data memory behind-the-scenes.



  • Thank you guys,

    that all I wanted to know.

    regards
    Oliver


  • Qt Champions 2017

    hhmmmm @JKSH You are right. It is my complete oversight by just looking the list clear.


Log in to reply