Bug 462277 - When a window is closed, kwin does not free the memory for its icon
Summary: When a window is closed, kwin does not free the memory for its icon
Status: RESOLVED NOT A BUG
Alias: None
Product: kwin
Classification: Plasma
Component: general (other bugs)
Version First Reported In: 5.26.3
Platform: Arch Linux Linux
: NOR normal
Target Milestone: ---
Assignee: KWin default assignee
URL:
Keywords: efficiency-and-performance
Depends on:
Blocks:
 
Reported: 2022-11-26 16:21 UTC by Kishore Gopalakrishnan
Modified: 2026-01-22 14:09 UTC (History)
3 users (show)

See Also:
Latest Commit:
Version Fixed/Implemented In:
Sentry Crash Report:


Attachments
kwin support information (6.26 KB, text/plain)
2022-11-26 16:21 UTC, Kishore Gopalakrishnan
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kishore Gopalakrishnan 2022-11-26 16:21:25 UTC
Created attachment 154051 [details]
kwin support information

SUMMARY
When a window is opened, kwin seems to allocate some memory to store its icon. However, this memory is not freed when the window is closed.

heaptrack output: https://drive.google.com/file/d/1xkhCrdzyPWCX6wtRDGubhhkyOfE-jeaN/view?usp=sharing (it is too big to attach here, 5 MB)

STEPS TO REPRODUCE
1. `heaptrack kwin_x11 --replace`
2. Run the following loop in bash:
```
while true
do
    sleep 5
    dolphin &
    sleep 5
    killall dolphin -u kishore
done
```
3. Wait for some time
4. Kill kwin.
5. `heaptrack_gui $name_of_output_file`

OBSERVED RESULT
Kwin's memory usage grows linearly with time (heaptrack output attached). The backtrace to which heaptrack associates most of the memory is:
```
readIcon in libKF5WindowSystem.so.5
NETWinInfo::update(QFlags<NET::Property>, QFlags<NET::Property2>) in libKF5WindowSystem.so.5
KWin::WinInfo::WinInfo(KWin::X11Window*, unsigned int, unsigned int, QFlags<NET::Property>, QFlags<NET::Property2>) in libkwin.so.5
KWin::X11Window::manage(unsigned int, bool) in libkwin.so.5
KWin::Workspace::createX11Window(unsigned int, bool) in libkwin.so.5
KWin::Workspace::workspaceEvent(xcb_generic_event_t*) in libkwin.so.5
QAbstractEventDispatcher::filterNativeEvent(QByteArray const&, void*, long*) in libQt5Core.so.5
QXcbConnection::handleXcbEvent(xcb_generic_event_t*) in libQt5XcbQpa.so.5
QXcbConnection::processXcbEvents(QFlags<QEventLoop::ProcessEventsFlag>) in libQt5XcbQpa.so.5
<unresolved function> in libQt5XcbQpa.so.5
g_main_context_dispatch in libglib-2.0.so.0
<unresolved function> in libglib-2.0.so.0
g_main_context_iteration in libglib-2.0.so.0
QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) in libQt5Core.so.5
QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) in libQt5Core.so.5
QCoreApplication::exec() in libQt5Core.so.5
main in kwin_x11
```

EXPECTED RESULT
Memory usage should reach steady state after some time.

SOFTWARE/OS VERSIONS
Operating System: Arch Linux
KDE Plasma Version: 5.26.3
KDE Frameworks Version: 5.100.0
Qt Version: 5.15.7
Kernel Version: 5.15.79-1-lts (64-bit)
Graphics Platform: X11
Graphics Processor: virgl
Manufacturer: QEMU

ADDITIONAL INFORMATION
A. I'm not sure if the same thing happens on Wayland.

B. I installed debug symbols for kwin and kwindowsystem.

C. kwin support information (`qdbus org.kde.KWin /KWin supportInformation`) is attached

D. heaptrack output: https://drive.google.com/file/d/1xkhCrdzyPWCX6wtRDGubhhkyOfE-jeaN/view?usp=sharing (it is too big to attach here, 5 MB)
Comment 1 Bug Janitor Service 2022-11-26 16:33:31 UTC
Thank you for the bug report!

Please note that Plasma 5.25.3 is not supported for much longer by KDE; supported versions are 5.24, and 5.26 or newer.

If at all possible please upgrade to a supported version and verify that the bug is still happening there.

If you're unsure how to do this, contact your distributor about it.
Comment 2 Fushan Wen 2022-12-01 15:51:32 UTC
Cannot reproduce
Comment 3 Kishore Gopalakrishnan 2023-01-08 11:27:32 UTC
(In reply to Fushan Wen from comment #2)
> Cannot reproduce

I can still reproduce this on 

Operating System: Arch Linux
KDE Plasma Version: 5.26.5
KDE Frameworks Version: 5.101.0
Qt Version: 5.15.8
Kernel Version: 5.15.86-1-lts (64-bit)
Graphics Platform: X11
mesa 22.3.2-3

I also noticed that the memory usage shown in ksysguard (for the kwin_x11 process) is much lower than that reported by heaptrack. Does this mean the leaks found by heaptrack are false positives?
Comment 4 Fushan Wen 2023-01-08 11:44:18 UTC
I find memory is not released when there is still plenty of free memory, so ksysguard may not report the accurate used memory.
Comment 5 David Edmundson 2023-03-17 16:41:08 UTC
I can't see who deletes:

```
icons[i].data = new unsigned char[size];
```

The array is cleaned up, but our array contains malloc'ed data.
It's cleaned up if we readIcons multiple times, but not in the NetWinInfo destructor
Comment 6 David Edmundson 2023-03-17 16:52:05 UTC
Edit, it's cleaned up in `refdec_nwi`
Comment 7 Vlad Zahorodnii 2026-01-22 14:09:55 UTC
The memory allocator may not release some memory, which unfortunately is expected, e.g. due to fragmentation, etc. But as for that code, it looks like it frees memory as expected, although it wouldn't hurt to port that code away from `new` and `delete`.