Add widget diff info to help debug memory issues

This commit is contained in:
Veronica Berglyd Olsen
2023-11-28 17:29:34 +01:00
parent 83c9fb96e9
commit 4e1fa01562
+13 -1
View File
@@ -230,8 +230,14 @@ class GuiMainStatus(QStatusBar):
## ##
def memInfo(self) -> None: # pragma: no cover 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 import tracemalloc
from collections import Counter
widgets = qApp.allWidgets()
if not self._debugInfo: if not self._debugInfo:
if tracemalloc.is_tracing(): if tracemalloc.is_tracing():
self._traceMallocRef = "Total" self._traceMallocRef = "Total"
@@ -239,6 +245,12 @@ class GuiMainStatus(QStatusBar):
self._traceMallocRef = "Relative" self._traceMallocRef = "Relative"
tracemalloc.start() tracemalloc.start()
self._debugInfo = True 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() mem = tracemalloc.get_traced_memory()
stamp = datetime.now().strftime("%H:%M:%S") stamp = datetime.now().strftime("%H:%M:%S")