Analyze mem leak

This commit is contained in:
David Rodenkirchen 2026-04-15 09:29:56 +02:00
parent a53e7100da
commit 68c51a09f8

View File

@ -1,9 +1,15 @@
import logging import logging
import tracemalloc
import sys import sys
from pathlib import Path from pathlib import Path
from uuid import uuid4 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 rio import App, Theme, Color, Font, ComponentPage, Session
from from_root import from_root 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.services.LocalDataService import LocalData
from src.ezgg_lan_manager.types.UserSession import UserSession from src.ezgg_lan_manager.types.UserSession import UserSession
tracemalloc.start(25)
logger = logging.getLogger("EzggLanManager") 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__": if __name__ == "__main__":
start_hourly_logger()
theme = Theme.from_colors( theme = Theme.from_colors(
primary_color=Color.from_hex("ffffff"), primary_color=Color.from_hex("ffffff"),
secondary_color=Color.from_hex("018786"), secondary_color=Color.from_hex("018786"),