Spaces:
Sleeping
Sleeping
Update utils/memory_manager.py
Browse files- utils/memory_manager.py +1 -23
utils/memory_manager.py
CHANGED
|
@@ -5,8 +5,7 @@ import gc
|
|
| 5 |
logger = logging.getLogger("webtapi.memory")
|
| 6 |
|
| 7 |
class MemoryManager:
|
| 8 |
-
def __init__(self
|
| 9 |
-
self.memory_limit = memory_limit_mb * 1024 * 1024 # Convert to bytes
|
| 10 |
self.warning_threshold = 0.8 # 80% of limit
|
| 11 |
|
| 12 |
def check_memory_usage(self):
|
|
@@ -15,12 +14,6 @@ class MemoryManager:
|
|
| 15 |
memory_info = process.memory_info()
|
| 16 |
return memory_info.rss # Resident Set Size in bytes
|
| 17 |
|
| 18 |
-
def is_memory_available(self, required_mb=100):
|
| 19 |
-
"""Check if enough memory is available"""
|
| 20 |
-
current_usage = self.check_memory_usage()
|
| 21 |
-
available_memory = self.memory_limit - current_usage
|
| 22 |
-
return available_memory >= (required_mb * 1024 * 1024)
|
| 23 |
-
|
| 24 |
def optimize_memory(self):
|
| 25 |
"""Free up memory by cleaning up and garbage collection"""
|
| 26 |
logger.info("Optimizing memory usage...")
|
|
@@ -28,25 +21,10 @@ class MemoryManager:
|
|
| 28 |
# Force garbage collection
|
| 29 |
gc.collect()
|
| 30 |
|
| 31 |
-
# Clear CUDA cache if available
|
| 32 |
-
try:
|
| 33 |
-
import torch
|
| 34 |
-
if torch.cuda.is_available():
|
| 35 |
-
torch.cuda.empty_cache()
|
| 36 |
-
except ImportError:
|
| 37 |
-
pass
|
| 38 |
-
|
| 39 |
current_usage_mb = self.check_memory_usage() / (1024 * 1024)
|
| 40 |
logger.info(f"Current memory usage: {current_usage_mb:.2f} MB")
|
| 41 |
|
| 42 |
return current_usage_mb
|
| 43 |
-
|
| 44 |
-
def should_use_ai(self, operation_mb=200):
|
| 45 |
-
"""Determine if AI operations should be attempted based on available memory"""
|
| 46 |
-
if not self.is_memory_available(operation_mb):
|
| 47 |
-
logger.warning(f"Insufficient memory for AI operation (needed: {operation_mb}MB)")
|
| 48 |
-
return False
|
| 49 |
-
return True
|
| 50 |
|
| 51 |
# Global instance
|
| 52 |
memory_manager = MemoryManager()
|
|
|
|
| 5 |
logger = logging.getLogger("webtapi.memory")
|
| 6 |
|
| 7 |
class MemoryManager:
|
| 8 |
+
def __init__(self):
|
|
|
|
| 9 |
self.warning_threshold = 0.8 # 80% of limit
|
| 10 |
|
| 11 |
def check_memory_usage(self):
|
|
|
|
| 14 |
memory_info = process.memory_info()
|
| 15 |
return memory_info.rss # Resident Set Size in bytes
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def optimize_memory(self):
|
| 18 |
"""Free up memory by cleaning up and garbage collection"""
|
| 19 |
logger.info("Optimizing memory usage...")
|
|
|
|
| 21 |
# Force garbage collection
|
| 22 |
gc.collect()
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
current_usage_mb = self.check_memory_usage() / (1024 * 1024)
|
| 25 |
logger.info(f"Current memory usage: {current_usage_mb:.2f} MB")
|
| 26 |
|
| 27 |
return current_usage_mb
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Global instance
|
| 30 |
memory_manager = MemoryManager()
|