Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QStringList on Linux seems the keep memory
Forum Updated to NodeBB v4.3 + New Features

QStringList on Linux seems the keep memory

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.0k Views 2 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
    stvokr
    wrote on last edited by
    #1

    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

    JKSHJ 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

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

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stvokr
        wrote on last edited by stvokr
        #3

        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.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          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).

          (Z(:^

          1 Reply Last reply
          4
          • S stvokr

            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

            JKSHJ Online
            JKSHJ Online
            JKSH
            Moderators
            wrote on last edited by JKSH
            #5

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

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            5
            • S Offline
              S Offline
              stvokr
              wrote on last edited by
              #6

              Thank you guys,

              that all I wanted to know.

              regards
              Oliver

              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #7

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

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                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