Extensions/comfyui-memory-manager
ComfyUI Extension

comfyui-memory-manager

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

By darshd9941·Created 3 months ago·Updated 3 months ago· 1
darshd9941/comfyui-memory-manager
Nodes5
On cloudLocal install
Categorymemory/monitor, memory/management
Stars1
Updated3 months ago
Readme

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.