Hello,

Im currently updating my application from Qt 5.2.1 with MinGW 4.8 to Qt 5.7 with MinGW 5.3. I am using the precompiled packages from the online installer under Windows 7. If I compile with Qt 5.7 I see a significant increase of memory consumption during runtime in the task manager from around 70 MB with Qt 5.2.1 to 380 MB with Qt 5.7. So I tried to find the reason for that behaviour.

I found that the QComboBox method http://doc.qt.io/qt-4.8/http://doc.qt.io/qt-5/qcombobox.html#addItem-1 overload that takes a QIcon is increasing the memory usage. After testing around, I think the memory increased is caused because http://doc.qt.io/qt-4.8/http://doc.qt.io/qt-5/qicon.html#addFile creates pixmaps for every Icon Mode and Icon State and load it into memory at initialisation and not like the documentation mentioned on demand.
Adds an image from the file with the given fileName to the icon, as a specialization for size, mode and state. The file will be loaded on demand.
I searched the internet about this problem, but the only thing I found was https://forum.qt.io/topic/72240/qico...xmap-on-demand. That suprised me, because a increase of the memory footprint for about 300% isn't something that you miss as a developer.

I prepared a minimal example thats shows the problem. I use PNG Files attached to the Qt resource system, but you can use files in the application directory as well.

Qt Code:
  1. #include <QApplication>
  2. #include <QWidget>
  3. #include <QComboBox>
  4. #include <QVBoxLayout>
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. //QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
  9. //QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, false);
  10.  
  11. QApplication a(argc, argv);
  12.  
  13. bool useIcon = true;
  14. bool usePixmap = false;
  15. QWidget mainWidget;
  16. QVBoxLayout *layout = new QVBoxLayout;
  17. for(int i = 0; i < 20; ++i)
  18. {
  19. QComboBox *box = new QComboBox;
  20. box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  21. if(useIcon)
  22. {
  23. if(usePixmap)
  24. {
  25. box->addItem(QIcon(QPixmap(":/img/about-icon.png")), QObject::tr("Box %1 - 1").arg(i));
  26. box->addItem(QIcon(QPixmap(":/img/android-add-contact.png")), QObject::tr("Box %1 - 2").arg(i));
  27. box->addItem(QIcon(QPixmap(":/img/Bug.png")), QObject::tr("Box %1 - 3").arg(i));
  28. box->addItem(QIcon(QPixmap(":/img/database-symbol-md.png")), QObject::tr("Box %1 - 4").arg(i));
  29. box->addItem(QIcon(QPixmap(":/img/Devices-printer-icon.png")), QObject::tr("Box %1 - 5").arg(i));
  30. }
  31. else
  32. {
  33. box->addItem(QIcon(":/img/about-icon.png"), QObject::tr("Box %1 - 1").arg(i));
  34. box->addItem(QIcon(":/img/android-add-contact.png"), QObject::tr("Box %1 - 2").arg(i));
  35. box->addItem(QIcon(":/img/Bug.png"), QObject::tr("Box %1 - 3").arg(i));
  36. box->addItem(QIcon(":/img/database-symbol-md.png"), QObject::tr("Box %1 - 4").arg(i));
  37. box->addItem(QIcon(":/img/Devices-printer-icon.png"), QObject::tr("Box %1 - 5").arg(i));
  38. }
  39. }
  40. else
  41. {
  42. box->addItem(QObject::tr("Box %1 - 1").arg(i));
  43. box->addItem(QObject::tr("Box %1 - 2").arg(i));
  44. box->addItem(QObject::tr("Box %1 - 3").arg(i));
  45. box->addItem(QObject::tr("Box %1 - 4").arg(i));
  46. box->addItem(QObject::tr("Box %1 - 5").arg(i));
  47. }
  48. layout->addWidget(box);
  49. }
  50. mainWidget.setLayout(layout);
  51. mainWidget.show();
  52.  
  53. return a.exec();
  54. }
To copy to clipboard, switch view to plain text mode 

I compiled a realease build and running this example with Qt 5.2.1 and Qt 5.7. on Windows 7. The memory consumption is measured using the windows task manager. These are the results:

  • useIcon = true, usePixmap = false:
    • Qt 5.2.1: 5740 KB
    • Qt 5.7: 46472 KB
  • useIcon = true, usePixmap = true:
    • Qt 5.2.1: 7184 KB
    • Qt 5.7: 9712 KB
  • useIcon = false:
    • Qt 5.2.1: 4636 KB
    • Qt 5.7: 8096 KB


My question are:
  • Can you reproduce this behaviour?
  • Is there a flag that controls if the pixmaps are loaded on demand or not?
  • Is there a workaround or should I fill out a bug report?


Thank you very much.
~ Markus