| import os
|
| import gc
|
| import torch
|
| import psutil
|
|
|
| def clear_system_memory():
|
|
|
| print("Clearing Python cache...")
|
| gc.collect()
|
|
|
|
|
| if torch.cuda.is_available():
|
| print("Clearing GPU cache...")
|
| torch.cuda.empty_cache()
|
| torch.cuda.ipc_collect()
|
|
|
|
|
| if os.name == 'posix':
|
| print("Clearing RAM caches...")
|
| os.system('sync && echo 3 > /proc/sys/vm/drop_caches')
|
|
|
|
|
| print("Killing high-memory processes...")
|
| for proc in psutil.process_iter(['pid', 'name', 'memory_info']):
|
| try:
|
| if proc.info['memory_info'].rss > 500 * 1024 * 1024:
|
| print(f"Killing process {proc.info['name']} (PID: {proc.info['pid']})")
|
| proc.terminate()
|
| except (psutil.NoSuchProcess, psutil.AccessDenied):
|
| continue
|
|
|
| print("Memory cleared successfully!")
|
|
|
|
|
| clear_system_memory()
|
|
|