From 68c51a09f82692a578a82c57288ae29dea57aa2d Mon Sep 17 00:00:00 2001 From: David Rodenkirchen Date: Wed, 15 Apr 2026 09:29:56 +0200 Subject: [PATCH] Analyze mem leak --- src/EzggLanManager.py | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/EzggLanManager.py b/src/EzggLanManager.py index 3436e43..b659cf3 100644 --- a/src/EzggLanManager.py +++ b/src/EzggLanManager.py @@ -1,9 +1,15 @@ import logging +import tracemalloc import sys from pathlib import Path from uuid import uuid4 +import gc +import time +import threading +from collections import Counter +from datetime import datetime, UTC from rio import App, Theme, Color, Font, ComponentPage, Session from from_root import from_root @@ -13,9 +19,49 @@ from src.ezgg_lan_manager.helpers.LoggedInGuard import logged_in_guard, not_logg from src.ezgg_lan_manager.services.LocalDataService import LocalData from src.ezgg_lan_manager.types.UserSession import UserSession +tracemalloc.start(25) + logger = logging.getLogger("EzggLanManager") +def log_object_summary(): + gc.collect() + + objs = gc.get_objects() + counter = Counter(type(obj).__name__ for obj in objs) + + timestamp = datetime.now(UTC).isoformat() + + with open("memory_objects.log", "a") as f: + f.write(f"\n=== {timestamp} ===\n") + f.write(f"Total objects: {len(objs)}\n") + + for name, count in counter.most_common(25): + f.write(f"{name}: {count}\n") + +def log_top_allocations(): + snapshot = tracemalloc.take_snapshot() + top_stats = snapshot.statistics('lineno') + + timestamp = datetime.now(UTC).isoformat() + + with open("memory_allocations.log", "a") as f: + f.write(f"\n=== {timestamp} ===\n") + for stat in top_stats[:10]: + f.write(str(stat) + "\n") + +def start_hourly_logger(): + def loop(): + while True: + log_object_summary() + log_top_allocations() + time.sleep(3) # 1 hour + + t = threading.Thread(target=loop, daemon=True) + t.start() + if __name__ == "__main__": + start_hourly_logger() + theme = Theme.from_colors( primary_color=Color.from_hex("ffffff"), secondary_color=Color.from_hex("018786"),