Stable-Hair / cahceclear.py
ahmdliaqat's picture
Upload 12 files
fd73d17 verified
import os
import gc
import torch
import psutil
def clear_system_memory():
# Clear Python cache
print("Clearing Python cache...")
gc.collect()
# Clear GPU cache if PyTorch is being used
if torch.cuda.is_available():
print("Clearing GPU cache...")
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
# Clear RAM (Only works on Linux/Mac for now)
if os.name == 'posix':
print("Clearing RAM caches...")
os.system('sync && echo 3 > /proc/sys/vm/drop_caches')
# List and terminate high-memory processes (use with caution)
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: # Threshold: 500MB
print(f"Killing process {proc.info['name']} (PID: {proc.info['pid']})")
proc.terminate()
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
print("Memory cleared successfully!")
# Execute the cleaning process
clear_system_memory()