Skip to main content

Overview

ComfyUI includes a sophisticated memory management system that automatically handles VRAM allocation, model loading/unloading, and memory optimization across different hardware configurations.

VRAM States

ComfyUI adapts its behavior based on available VRAM through the VRAMState enum:

State Selection

The VRAM state is automatically detected based on:
  • Total VRAM available
  • Command-line arguments (--lowvram, --highvram, --novram, --gpu-only)
  • Hardware type (CPU, CUDA, MPS, XPU, etc.)

Device Management

Supported Devices

ComfyUI supports multiple compute backends:
Primary device for NVIDIA GPUs with full feature support.

Getting Memory Information

The memory reporting differs between hardware backends. CUDA uses torch.cuda.mem_get_info(), while XPU and NPU use their respective APIs.

Model Loading System

LoadedModel Class

The LoadedModel class tracks loaded models and their memory usage:

Loading Models to GPU

The load_models_gpu() function orchestrates model loading:
1

Memory Calculation

Calculate total VRAM and RAM requirements for all models
2

Free Memory

Unload existing models if insufficient memory
3

Partial Loading

Load only essential parts of models in low VRAM scenarios
4

Track Models

Add loaded models to current_loaded_models list

Partial Model Loading (LowVRAM)

In low VRAM scenarios, ComfyUI loads only portions of models:
MIN_WEIGHT_MEMORY_RATIO is set to 0.0 for NVIDIA (allowing more aggressive offloading) and 0.4 for other devices.

Memory Freeing Strategy

The free_memory() function intelligently unloads models:

Unloading Priority

Models are prioritized for unloading based on:
  1. Offloaded memory (models with more already offloaded)
  2. Reference count (fewer references = unload first)
  3. Total memory (larger models considered)

Reserved Memory

ComfyUI reserves memory for system operations:
Windows requires more reserved VRAM due to shared memory management. You can override this with --reserve-vram argument.

Async Weight Offloading

ComfyUI supports asynchronous weight offloading with CUDA/XPU streams:

Cast Buffers

To reduce memory allocations, ComfyUI maintains reusable cast buffers:

Pinned Memory

For faster CPU-GPU transfers, ComfyUI pins CPU memory:
Pinned memory enables faster DMA transfers but is limited to ~95% of system RAM on Linux and ~45% on Windows.

Device Placement Functions

ComfyUI provides specialized functions for different model components:

UNet/Diffusion Models

Text Encoders

VAE

Garbage Collection

ComfyUI monitors for memory leaks and triggers garbage collection:
If you see memory leak warnings, check for circular references in your custom nodes or models.

Command-Line Options

boolean
Enable low VRAM mode for GPUs with limited memory
boolean
Keep models in VRAM (disables offloading)
boolean
Minimal VRAM usage (very slow)
boolean
Never offload models to CPU
float
Reserve N GB of VRAM for other applications
boolean
Disable intelligent memory management
boolean
Disable async weight offloading
integer
Number of streams for async offloading (default: 2)
boolean
Disable pinned memory for CPU-GPU transfers

Best Practices

Monitor VRAM Usage

Use get_free_memory() to track available VRAM during execution

Avoid Memory Leaks

Ensure proper cleanup in custom nodes using weak references

Use Appropriate VRAM State

Let ComfyUI auto-detect or manually set based on your workflow

Enable Async Offload

Keep async offloading enabled for better performance on NVIDIA/AMD

Quantization

Reduce model memory footprint with FP8/FP16 quantization

Custom Nodes

Build memory-efficient custom nodes