Nodes/MD Nodes/MD: VRAM Canary (Memory Guardian)
ComfyUI Node

MD: VRAM Canary (Memory Guardian)

Keep long ComfyUI queues from dying on you

By MDMAchine·Created about a year ago·Updated 3 months ago· 15
MD: VRAM Canary (Memory Guardian)
  • any_input
  • passthrough
  • vram_stats
vram_threshold_gb2.0
auto_cleanuptrue
gpu_device0
verbose_modefalse
emergency_modefalse

Nothing kills a 30-minute upscale queue quite like an out-of-memory error on image 12. The VRAM Canary is the tripwire for that: it watches how much free VRAM you have left right before a heavy node runs, and if the number drops below your threshold it proactively cleans up instead of letting the next tensor blow past the edge.

How it works

You drop it into the graph, and it passes whatever it's guarding straight through. The clever bit is the any_input port - it's typed *, so it accepts a MODEL, LATENT, IMAGE, anything, and emits it unchanged on the other side. That's how it earns its place in the graph without disrupting anything: it's a toll booth, not a checkpoint.

Inside, it reads GPU memory with PyTorch's own torch.cuda.mem_get_info - no extra monitoring service needed. When free VRAM dips under vram_threshold_gb and auto_cleanup is on (it is by default), it calls ComfyUI's own soft_empty_cache - the same safe, non-destructive cache flush ComfyUI does after a queue run. So it's not doing anything exotic; it's just doing it at the right moment, mid-graph, before the big allocation.

The inputs that matter

Three of them. any_input (place it immediately before an upscaler or sampler), vram_threshold_gb, and auto_cleanup. The tooltip's guidance for the threshold is solid: 1.5–2.0 GB on an 8 GB card, 2.0–3.0 on 12 GB, 3.0–4.0 on 24 GB. Higher is more aggressive, which costs you speed from constant flushing; lower risks an OOM.

The optional emergency_mode is the heavy hammer - it unloads all models from VRAM to RAM when triggered. That frees the most memory, but every model has to reload, so only reach for it on massive 4K+ upscale runs. There's also gpu_device for multi-GPU rigs (default 0) and verbose_mode to print before/after byte counts when you're profiling.

Outputs are passthrough (your untouched data) and vram_stats, a STRING with the current state if you want to log it.

Installing it

It's part of the MD Nodes pack:

cd path/to/ComfyUI/custom_nodes
git clone https://github.com/MDMAchine/ComfyUI_MD_Nodes.git
cd ComfyUI_MD_Nodes
pip install -r requirements.txt

Or search MD_Nodes in ComfyUI Manager. Restart after.

The honest take

This is a guardrail, not a fix. If you're hitting the threshold every single run, the answer isn't a bigger canary - it's lowering your batch size, switching to tiled VAE decode, or unloading models you're done with. And it's NVIDIA/CUDA-only for the actual monitoring; on a CPU-only setup it just reports that CUDA isn't available and passes your data through. On a machine where VRAM is tight and queues are long, though, it genuinely saves runs. One less "welp, rerun the batch" moment.

CategoryMD_Nodes/Utility

Inputs (6)

NameTypeDefaultDescription
any_input*UNIVERSAL INPUT • Purpose: The data stream to guard. • Support: Accepts any data type (MODEL, IMAGE, LATENT, etc.). • Effect: Passed through unchanged after the VRAM check completes. ⭐ Recommended: Place immediately before heavy nodes (Upscalers, Samplers).
vram_threshold_gbFLOAT2.00.5–48VRAM THRESHOLD (GB) • Purpose: Triggers cleanup when free VRAM drops below this number. • Trade-offs: Higher values = more aggressive cleanup. Lower values = risk OOM errors. ⭐ Guidelines: - 8GB GPU: 1.5 - 2.0 GB - 12GB GPU: 2.0 - 3.0 GB - 24GB GPU: 3.0 - 4.0 GB
auto_cleanupBOOLEANtrueAUTO CLEANUP • Purpose: Automatically trigger memory cleanup when threshold is breached. • Effect: Executes 'soft_empty_cache' (safe, non-destructive). • Trade-offs: Disable only if actively debugging a suspected memory leak. ⭐ Recommended: Enabled for all production workflows.
gpu_deviceoptINT00–7GPU DEVICE INDEX • Purpose: Selects which GPU to monitor (0 = Primary). • Note: Only relevant for multi-GPU systems. ⭐ Recommended: 0.
verbose_modeoptBOOLEANfalseVERBOSE MODE • Purpose: Show detailed memory statistics in the console. • Effect: Prints before/after cleanup byte comparisons. ⭐ Recommended: False, unless profiling a heavy workflow.
emergency_modeoptBOOLEANfalseEMERGENCY MODE • Purpose: Engages aggressive memory cleanup if triggered. • Effect: Unloads all models from VRAM directly to RAM. • Trade-offs: Slower execution (models must reload), but frees maximum memory. ⭐ Recommended: False, use only for massive 4K+ upscaling.

Outputs (2)

NameTypeDescription
passthrough*
vram_statsSTRING