Nodes/ComfyUI-ZML-Image/ZML_清理内存显存
ComfyUI Node

ZML_清理内存显存

Nuke the VRAM between runs without restarting ComfyUI

By zml-w·Created about a year ago·Updated 2 months ago· 218
ZML_清理内存显存
  • 任意数据
  • 输出
清理显存true
清理内存false

ComfyUI has a way of slowly strangling itself over a long session: model stays loaded, cached tensors pile up, and by the fifth workflow the thing that used to run in 6GB now OOMs at 9. ZML_MemoryCleaner is the "flush it all" button you can drop into the graph - a node whose entire job is to unload models, force garbage collection, and clear the CUDA cache.

The README positions it plainly in the 2026.03 update: "添加了个清理显存和内存的节点." It's a maintenance node, not a fancy one. Which is fine - maintenance is exactly what long ComfyUI sessions need.

What it actually does

Two independent switches:

  • 清理显存 (default on) - the heavy one. It calls comfy.model_management.unload_all_models() to drop everything from VRAM, runs gc.collect(), then soft_empty_cache() to release the CUDA cache. This is the standard "give me my VRAM back" sequence ComfyUI itself uses, just packaged as a node.
  • 清理内存 (default off) - system RAM. This one's platform-specific and a little more aggressive: on Windows it calls into kernel32/psapi to empty process working sets and trim the system file cache; on Linux it runs malloc_trim and (with root) writes to /proc/sys/vm/drop_caches. It's the kind of thing that can help on machines where the OS is holding freed pages, but it's not magic - and on Linux without root, the drop_caches part just silently fails (the code catches the error and moves on).

There's also an optional 任意数据 input that passes straight through to the 输出 output. That's a deliberate design choice: it lets you insert the cleaner inline in a chain - the data flows through it while the cleanup happens - so the node doesn't force you to rewire your graph just to add a memory flush at a specific point in the pipeline.

When to use it

The honest answer: more than you think you need to. The classic pattern is at the end of a memory-hungry branch - after a big upscale, a batch render, or before a model swap - so the next branch starts clean instead of inheriting the last one's footprint. It's also the right node to wire in when you're chaining multiple model families (SDXL → FLUX → video) in one session and ComfyUI isn't releasing memory between them.

The caveat: unloads cost time. Every unload_all_models() means the next sampler has to re-load the model from disk, so dropping this node between every single step will make a long workflow noticeably slower. Use it at the seams - the places where you'd otherwise be reaching for a restart.

Install

Ships in ComfyUI-ZML-Image:

cd ComfyUI/custom_nodes
git clone https://github.com/zml-w/ComfyUI-ZML-Image
# restart ComfyUI

or ComfyUI Manager → search "ComfyUI-ZML-Image". It uses psutil (in the pack's requirements) and the ComfyUI model-management internals - nothing exotic. Chinese-first UI; translation patch at https://github.com/zml-w/ZZZ_ZML_English_Patch.

One honest warning: the Windows "empty working set" loop iterates over every running process and could nudge other apps' memory. It's low-risk and the code guards everything with try/except, but if you're running something important in parallel, maybe leave 清理内存 off and rely on 清理显存 - that's the switch that does 95% of the useful work anyway.

Categoryimage/ZML_图像/工具

Inputs (3)

NameTypeDefaultDescription
清理显存BOOLEANtrue清理GPU显存
清理内存BOOLEANfalse清理系统内存
任意数据opt*

Outputs (1)

NameTypeDescription
输出*