Possible memory leak when opening QMenu in PyQt5
Phil Thompson
phil at riverbankcomputing.com
Tue May 19 21:51:18 BST 2026
On 14/05/2026 13:08, Alexander Stamatov wrote:
> Hello, I am observing a large consistent increase in RAM every time a
> nested QMenu is opened in my PyQt5 application.
>
> Even with a simplified example using a nested menu and no external
> images, the RAM usage grows with each interaction and does not appear
> to be reclaimed. If you do not nest QMenus, nothing will happen, and
> the memory will be deallocated as expected. I have attached a minimal
> reproducible example and demonstrating the problem, here is the link
> to a video demonstrating it on my pc:
> https://drive.google.com/file/d/1pfactub-1R-gPSmARAOmKe9RgujfQdbx/view?usp=sharing
>
> To reproduce: Run the script, Double-Middle-click the blue square,
> select one of the options from the inner menu and repeat a few times.
> Finally, observe the console output; the RAM usage increases on each
> open/close cycle, while the Qt object count remains constant.
>
> Environment: Python 3.13.9, PyQt5 version: 5.15.11, Operating System:
> Windows 11 Pro.
>
> Is this expected behavior due to internal Qt caching, or is there a
> specific way to ensure these resources are released?
That seems to be a Qt question rather than a PyQt question.
The PyQt side doesn't do much once the GUI is set up. I've attached a
simplified version of your test which minimises the PyQt involvement as
much as possible. I would hope(!) that this demonstrates similar
behaviour.
Phil
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PyQt5_Memory_Leak_Menu.py
Type: text/x-script.python
Size: 1245 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20260519/9a07e8ff/attachment.bin>
More information about the PyQt
mailing list