1

I'm trying to create an application that hangs in the tray and shows the window on demand. This window consumes a lot of memory, so I want to release all my memory when I close the window.

I'm trying to do this in the following way:

QApplication a(argc, argv);

auto trayMenu = new QMenu;
trayMenu->addAction(QIcon::fromTheme("folder"), "Open main window", [] {
    auto mainWindow = new MainWindow;
    mainWindow->show();
});

QSystemTrayIcon trayIcon(QIcon::fromTheme("folder"));
trayIcon.setContextMenu(trayMenu);
trayIcon.show();

return QApplication::exec();

In the MainWindow constructor, I specified the delete attribute on closing:

setAttribute(Qt::WA_DeleteOnClose);

At the start the application consumes ~5 MB. After opening the window the application consumes ~170 MB. But after the window is closed, the consumption does not change. Also, when I reopen and close the window, the application continues to consume as much memory (nothing changes). Is it possible to completely release all the memory of the window? I'm using Linux.

Shatur95
  • 93
  • 1
  • 9
  • 1
    did you forget to `delete` stuff in the `MainWindow` destructor? – perivesta Jan 17 '19 at 10:43
  • Linux has a habit of not releasing memory pages as the application free memory. That will look like the applications use more memory than they do, or even leak memory. Rest assured, as long as you make sure to free the resource you have allocated, the memory isn't really allocated. It's a false positive. – Some programmer dude Jan 17 '19 at 10:46
  • 1
    Related: [Why does the free() function not return memory to the operating system?](https://stackoverflow.com/questions/52417318/why-does-the-free-function-not-return-memory-to-the-operating-system) – Yksisarvinen Jan 17 '19 at 11:13
  • @dave, all my widgets are children of MainWindow and are deleted automatically... – Shatur95 Jan 17 '19 at 11:45
  • @Yksisarvinen, interesting... So, I can't return memory to operation system manually? – Shatur95 Jan 17 '19 at 11:45
  • When you delete or otherwise free objects, the memory *is* returned to the operating system. The operating system just doesn't remove the pages from the process. You should probably read a little more about virtual memory and how it works to understand it better. But rest assured that once you release your resources, they really are released. – Some programmer dude Jan 17 '19 at 11:49
  • @Someprogrammerdude, Thanks for the information! – Shatur95 Jan 17 '19 at 11:58
  • Did you check you actually delete your window when you minimize it to the system tray ? Like with a breakpoint in the destructor. From what I read, the window is not deleted when you only minimize it (even in the system tray, depending on your implementation). See https://stackoverflow.com/q/3332257/6165833 – ymoreau Jan 29 '19 at 12:43

0 Answers0