Spaces:
Sleeping
Sleeping
| import psutil | |
| import logging | |
| import gc | |
| logger = logging.getLogger("webtapi.memory") | |
| class MemoryManager: | |
| def __init__(self): | |
| self.warning_threshold = 0.8 # 80% of limit | |
| def check_memory_usage(self): | |
| """Check current memory usage""" | |
| process = psutil.Process() | |
| memory_info = process.memory_info() | |
| return memory_info.rss # Resident Set Size in bytes | |
| def optimize_memory(self): | |
| """Free up memory by cleaning up and garbage collection""" | |
| logger.info("Optimizing memory usage...") | |
| # Force garbage collection | |
| gc.collect() | |
| current_usage_mb = self.check_memory_usage() / (1024 * 1024) | |
| logger.info(f"Current memory usage: {current_usage_mb:.2f} MB") | |
| return current_usage_mb | |
| # Global instance | |
| memory_manager = MemoryManager() |