Fixed some errors I made with cleanup.
This commit is contained in:
+12
-6
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user