darshd9941 345f86f5ba feat: ComfyUI Memory Manager — prevent OOM crashes with smart model unloading
- VRAM Monitor node: real-time GPU memory stats
- Force Cleanup node: gc + CUDA cache clear
- Memory Budget Gate: block if insufficient VRAM
- LRU Model Cache: auto-evict least-recently-used models
- Cache Stats node: inspect loaded models
- Set Cache Size node: configure VRAM budget
2026-05-01 14:59:33 +05:30

ComfyUI Memory Manager

ComfyUI custom nodes for preventing OOM crashes — real-time VRAM monitoring, automatic model unloading, LRU cache, and memory budget gates.

Python ComfyUI License

The Problem

ComfyUI hordes VRAM until it crashes. Even on a 4090 with 64GB RAM, complex workflows with multiple model loads (base + refiner + upscaler + LoRAs) exceed hardware limits. The built-in "Clear RAM" nodes don't work. You must manually separate workflow stages and clear between them.

The Solution

Drop these nodes into your workflow to manage memory automatically:

# Copy to ComfyUI custom_nodes/
cp -r comfyui-memory-manager/ ComfyUI/custom_nodes/

Nodes

Node Description
📊 VRAM Monitor Real-time VRAM usage display
🧹 Force Cleanup Garbage collection + CUDA cache clear
🚦 Memory Budget Gate Block execution if insufficient VRAM
⚙️ Set Cache Size Set max VRAM budget for model cache
📈 Cache Stats Show cached models and their sizes

Usage

Monitor VRAM

Add VRAM Monitor to any point in your workflow. It outputs a string with current VRAM stats:

VRAM: 8234MB / 16384MB (50.2%)
Reserved: 9100MB | Free: 7284MB

Auto-Cleanup Between Stages

Connect ForceCleanup between model-heavy stages:

[Load Checkpoint] → [Force Cleanup] → [Load LoRA] → [KSampler]

Memory Budget Gate

Prevent OOM by gating expensive operations:

[Load SDXL] → [Memory Budget Gate (required_mb=4000)] → [KSampler]
                ↓ status: "OK — 7284MB available (need 4000MB)"

LRU Model Cache

The SetCacheSize node configures an LRU cache that automatically evicts least-recently-used models when VRAM fills up.

Installation

cd ComfyUI/custom_nodes
git clone https://github.com/darshd9941/comfyui-memory-manager.git
cd comfyui-memory-manager
pip install -r requirements.txt

How It Works

  1. VRAM Monitor queries torch.cuda for real-time memory stats
  2. Force Cleanup runs gc.collect() + torch.cuda.empty_cache() + torch.cuda.synchronize()
  3. Memory Budget Gate checks free VRAM before allowing downstream nodes to execute
  4. LRU Cache tracks loaded models and evicts least-recently-used ones when budget exceeded
  5. Cache Stats inspects the LRU cache to show what's loaded

Requirements

  • ComfyUI (latest)
  • PyTorch with CUDA
  • Python 3.9+

License

MIT License — see LICENSE for details.

S
Description
No description provided
Readme MIT
30 KiB
Languages
Python 100%