Fixed some errors I made with cleanup.

This commit is contained in:
Storm Dragon
2025-11-13 01:31:59 -05:00
parent 671a290323
commit 9c63a21804
2 changed files with 15 additions and 49 deletions
+12 -6
View File
@@ -813,17 +813,21 @@ class BookReader:
pygame.event.clear()
# Alternate between fast (gen 0) and full GC
if gcCounter % 600 == 0:
# At 300: gen 0 only (fast)
# At 600: full collection (all generations)
if gcCounter >= 600:
gc.collect() # Full collection every 20 seconds
gcCounter = 0 # Reset counter after full GC
else:
gc.collect(generation=0) # Fast collection every 10 seconds
gc.collect(generation=0) # Fast collection at 10 seconds
# Don't reset - let it reach 600 for full GC
# Memory watchdog: warn if exceeding 2GB (50% on Pi 4GB)
try:
import resource
# pylint: disable=no-member
memUsage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024 # MB
# More aggressive memory warnings and cleanup
if memUsage > 1536: # 1.5GB threshold
if not memoryWarningShown:
@@ -836,17 +840,19 @@ class BookReader:
for surfaceType, surface, position in self.cachedSurfaces:
del surface
self.cachedSurfaces.clear()
# Force mpv player cleanup
if hasattr(self, 'audioPlayer') and self.audioPlayer:
self.audioPlayer._cleanup_temp_files()
# Additional garbage collection
gc.collect()
gc.collect() # Second pass
if memUsage > 2048 and not memoryWarningShown:
memoryWarningShown = True
self.speechEngine.speak("Warning: High memory usage detected. Consider restarting BookStorm soon.")
except Exception as e:
print(f"Memory monitoring error: {e}")
pass
gcCounter = 0
# Limit to 30 FPS to avoid CPU spinning
clock.tick(30)