From 4e1fa01562122cfaa8a0ad2e14f8a14725999acc Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Tue, 28 Nov 2023 17:29:34 +0100 Subject: [PATCH] Add widget diff info to help debug memory issues --- novelwriter/gui/statusbar.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/novelwriter/gui/statusbar.py b/novelwriter/gui/statusbar.py index 1f35ffb8..1a4387ab 100644 --- a/novelwriter/gui/statusbar.py +++ b/novelwriter/gui/statusbar.py @@ -230,8 +230,14 @@ class GuiMainStatus(QStatusBar): ## def memInfo(self) -> None: # pragma: no cover - """Display memory info on the status bar.""" + """Display memory info on the status bar. This is used to + investigate memory usage and Qt widgets that get left in memory. + Enabled by the --meminfo command line flag. + """ import tracemalloc + from collections import Counter + + widgets = qApp.allWidgets() if not self._debugInfo: if tracemalloc.is_tracing(): self._traceMallocRef = "Total" @@ -239,6 +245,12 @@ class GuiMainStatus(QStatusBar): self._traceMallocRef = "Relative" tracemalloc.start() self._debugInfo = True + self._wCounts = Counter([type(x).__name__ for x in widgets]) + + if hasattr(self, "_wCounts"): + diff = Counter([type(x).__name__ for x in widgets]) - self._wCounts + for name, count in diff.items(): + logger.debug("Widget '%s': +%d", name, count) mem = tracemalloc.get_traced_memory() stamp = datetime.now().strftime("%H:%M:%S")