Nodes/ComfyUI-Xz3r0-Nodes/XMemoryCleanup
ComfyUI Node

XMemoryCleanup

A fire escape for when ComfyUI is eating your VRAM

By Xz3r0-M·Created 8 months ago·Updated about 6 hours ago· 13
XMemoryCleanup
  • anything
  • anything
cleanup_memoryfalse
cleanup_node_usagefalse
cleanup_vramfalse

ComfyUI has a habit of holding onto models and VRAM long after you're done with them, and on a small card that's the difference between a smooth run and an OOM crash mid-batch. XMemoryCleanup is the pack's manual pressure-release valve: a node with three cleanup switches - Python garbage collection, model unloading, and VRAM cache clearing - that also passes data through so you can drop it into a workflow where a cleanup step belongs. It's the kind of node you reach for when a long workflow gradually eats memory and you want a checkpoint between heavy stages.

How it works

Three independent toggles, all defaulting to off - nothing runs until you deliberately flip one:

  • cleanup_memory - runs Python's gc.collect() to reclaim reclaimable Python-side memory. Cheap, safe, and rarely sufficient on its own.
  • cleanup_node_usage - unloads currently loaded models and cleans up their memory usage. This is the big hammer: after it runs, the next stage has to reload the model from disk, which is slow but frees real VRAM.
  • cleanup_vram - clears the VRAM cache through ComfyUI's model management without unloading currently loaded models. The middle option: frees cached tensors while keeping the active model resident.

The execution logic is direct - check the toggle, run the action, and if none are enabled it just passes the data through untouched. It's marked not_idempotent, because its side effects change system state rather than producing a pure value. There's also a companion API route (/xz3r0/xmemorycleanup/action) that the pack's frontend extension can call, so you can trigger the same cleanups from a button rather than only via graph execution.

The pass-through is the detail that makes it usable: anything in, anything out, type-preserving via MatchType. You can wire it into a chain so the cleanup happens at a specific point in execution - say, after a big VAE decode and before the next sampler stage - rather than hoping a background thread does the right thing. If you don't need the passthrough, you can also just park it as a node with nothing connected and toggle it when you want a manual flush.

Inputs and outputs

  • cleanup_memory / cleanup_node_usage / cleanup_vram - the three switches.
  • anything - optional pass-through input.
  • anything - the unchanged pass-through output.

Installing it

Part of ComfyUI-Xz3r0-Nodes. ComfyUI Manager: search ComfyUI-Xz3r0-Nodes, or:

cd ComfyUI/custom_nodes
git clone https://github.com/Xz3r0-M/ComfyUI-Xz3r0-Nodes.git
cd ComfyUI-Xz3r0-Nodes
pip install -r requirements.txt

Restart ComfyUI. It talks to ComfyUI's own model_management and Python's gc - no extra dependencies.

Gotchas

The big one is the unload toggle: cleanup_node_usage frees VRAM, but the next model load pays for it with a long reload stall, so don't put it in the hot path of every run. The KB's troubleshooting writeup is the right framing - moving data between VRAM and RAM is expensive, so "just unload everything" is rarely a free win; prefer cleanup_vram (cache clear, keep model) as the first thing to try. And because all three default to off, a node that looks like it should be protecting you is doing nothing until you configure it - which is intentional (the author doesn't want surprise slowdowns) but easy to misread. Also worth knowing: this is a manual tool. ComfyUI already does background cache management; use this when that's not enough, not as a permanent fixture in every workflow.

Category♾️ Xz3r0/Workflow-Processing

Inputs (4)

NameTypeDefaultDescription
cleanup_memoryBOOLEANfalseRun Python garbage collection (gc.collect) to release reclaimable Python-side memory
cleanup_node_usageBOOLEANfalseUnload currently loaded models and clean up model memory usage
cleanup_vramBOOLEANfalseClear VRAM cache via ComfyUI model management without unloading currently loaded models
anythingoptCOMFY_MATCHTYPE_V3Optional pass-through input. Output type matches the connected input type automatically.

Outputs (1)

NameTypeDescription
anythingCOMFY_MATCHTYPE_V3Original input data passed through unchanged