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 theVRAMState 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:- CUDA (NVIDIA)
- MPS (Apple)
- XPU (Intel)
- NPU (Ascend)
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
TheLoadedModel class tracks loaded models and their memory usage:
Loading Models to GPU
Theload_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 listPartial 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
Thefree_memory() function intelligently unloads models:
Unloading Priority
Models are prioritized for unloading based on:- Offloaded memory (models with more already offloaded)
- Reference count (fewer references = unload first)
- Total memory (larger models considered)
Reserved Memory
ComfyUI reserves memory for system operations: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: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: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 executionAvoid 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
Related
Quantization
Reduce model memory footprint with FP8/FP16 quantization
Custom Nodes
Build memory-efficient custom nodes