Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-134956

QMap holding memory not release to OS

    XMLWordPrintable

Details

    • Bug
    • Resolution: Out of scope
    • P2: Important
    • None
    • 6.7.2
    • None
    • Linux - Centos 9
    • Linux/Other display system

    Description

      QMap not release memory to OS after program running for a while.

      I have create a simple situation.

      • Create a 100 threads, each thread loop 10000 and doing 2 two function below
        inline void raiseMemory(const qint64 numLoops)
        {
            auto data = QMap<std::string, QByteArray>({});
            for (int i = 0; i < numLoops; ++i) {
                data.insert(std::to_string(i), QByteArray(1024, 'D'));
            }
        }
        inline void raiseCpu(const qint64 numLoops)
        {
            uint64_t sum = 0;
            for (uint64_t i = 0; i < numLoops; ++i) {
                sum += i * i % 123456789;
            }
        } 
      • The main thread:
        // [1] Start threads
        qInfo() << "Sleeping for 3s before starting threads";
        QThread::msleep(3000);
        qInfo() << "Starting threads";
        QList<QThread*> threads;
        for (int i = 0; i < inArgs.numThreads; ++i) {
            QThread* thread = QThread::create([&inArgs]() {
                raiseCpu(inArgs.numLoops);
                raiseMemory(inArgs.numLoops);
            });
            threads.append(thread);
            thread->start();
        }// [2] Wait for threads to finish
        for (QThread* thread : threads) {
            thread->wait();
        }
        qInfo() << "All threads finished. Sleeping for 3s before deleting threads.";
        QThread::msleep(3000);// [3] Delete threads
        for (QThread* thread : threads) {
            delete thread;
        }
        qInfo() << "All threads deleted. Sleeping for 3s before finishing.";
        QThread::msleep(3000); 

      After running I got a chart – the memory does not go down

       

      I replaced QMap with QVector, QList and the memory will drop immediately when the threads finish their work.

      I checked Qt documentation but couldn't find any documentation related to this issue.

      And I think this is Qt BUG as the behavior of each container class is different.

      Please check this out

       

      My sample source code attached below

      Attachments

        1. image-2025-03-20-17-28-33-191.png
          105 kB
          Hieu Nguyen Duy
        2. sample-qmap.zip
          1 kB
          Hieu Nguyen Duy
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            era00 Hieu Nguyen Duy
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes