Nodes/ComfyUI-ZhiHui/🧠 智绘_显存内存优化
ComfyUI Node

🧠 智绘_显存内存优化

The node that unloads your models before you OOM

By zhuyungen·Created 8 months ago·Updated 4 months ago· 0
🧠 智绘_显存内存优化
  • 🔌 任意输入
  • 🔌 任意输出
◄✅ 启用true►
◄🐙 预留显存GB0.6►
◄🧠 内存安全余量GB4.0►
◄🧠 显存安全余量GB0.0►
◄🧹 低内存时卸载全部模型true►
◄🧽 运行时清空缓存true►
◄🧯 强制GCtrue►

You know the routine: big checkpoint, high-resolution sampling, and somewhere around the second pass the graph dies with CUDA out of memory. ZH_MemoryOptimizer ("🧠 智绘_显存内存优化") is a pre-emptive bouncer for that moment. Drop it into a workflow and it watches free RAM and VRAM, unloads models when things get tight, clears PyTorch's cache, and runs garbage collection - before the OOM, not after.

It's from ComfyUI-ZhiHui, a Chinese-language utility pack that inherited this node from Dapao-Toolbox (the pack's integration guide says so explicitly). It's genuinely a Dapao node wearing a "智绘" label - right down to the dapao.memopt.info message it sends to the frontend. The English community has never heard of any of this, so expect the UI text to be Chinese and the behavior to be unremarkable-but-handy.

How it works

The node is a pass-through: one ANY input, one ANY output, so you can wire it into a data flow without interrupting it. On each run it:

  1. Sets EXTRA_RESERVED_VRAM on ComfyUI's model management, using your 🐙 预留显存GB value - that's headroom you're deliberately keeping free for the OS or other apps. Bigger = safer but less room for models.
  2. If free RAM falls below 🧠 内存安全余量GB, it unloads all loaded models, frees CPU memory, and GCs. There's a separate 🧠 显存安全余量GB threshold that triggers a more surgical VRAM free.
  3. Runs soft_empty_cache() (the 🧽 运行时清空缓存 toggle) and gc.collect() (the 🧯 强制GC toggle) to fight fragmentation.

The author's framing in the node description is worth quoting in spirit: put it at the start of a workflow, or right before loading a big model / high-res sample - and because it's an output node that always runs, the status line ("动作=…", "预留显存=…", RAM/VRAM numbers) shows up right on the node after execution. It also declares IS_CHANGED returning NaN, so it fires every run instead of being skipped by the cache.

The inputs that matter

  • ✅ 启用 - master switch; off means "only report status, do nothing."
  • 🐙 预留显存GB (default 0.6), 🧠 内存安全余量GB (default 4), 🧠 显存安全余量GB (default 0 = rule disabled).
  • 🧹 低内存时卸载全部模型 - the aggressive one. Unloading everything guarantees a reload on the next model use, so it trades latency for safety. Leave it on if you OOM a lot, off if you hate wait-spikes.
  • 🔌 任意输入 / 任意输出 - the passthrough pair that lets you pin exactly where in the graph it fires.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/zhuyungen/ComfyUI-ZhiHui.git
pip install psutil

Restart ComfyUI. psutil is a hard requirement for this node - the module imports it at the top, so if it's missing the whole tools module fails to load and every node in that category disappears from your menu. Install it and reload.

Where people get burned

First, the psutil thing above - the README lists it as optional in places, but for this node it's not. Second, the unload-all-on-low-RAM behavior is blunt: in a workflow that churns through several models, the reload cost can make runs slower even as they get stabler - tune the RAM threshold rather than just enabling everything. Third, EXTRA_RESERVED_VRAM is a global, so this node changes memory behavior for the whole ComfyUI process, not just its own branch. And it can't conjure VRAM: if your model genuinely doesn't fit even empty, no amount of caching is saving you.

Category智绘灵箱

Inputs (8)

NameTypeDefaultDescription
✅ 启用BOOLEANtrue总开关;关闭时不做任何优化,仅输出状态
🐙 预留显存GBFLOAT0.60–256预留给系统/其它程序的显存(GB)。越大越稳,但可用显存越少
🧠 内存安全余量GBFLOAT4.00–512RAM可用低于此值(GB)时触发“卸载全部模型+清理”
🧠 显存安全余量GBFLOAT0.00–256VRAM可用低于此值(GB)时触发“卸载部分模型占用+清理”;0表示不启用该规则
🧹 低内存时卸载全部模型BOOLEANtrue当RAM不足时卸载所有已加载模型,回收更彻底但会触发后续重新加载
🧽 运行时清空缓存BOOLEANtrue每次运行都清理一次缓存,缓解碎片,但可能略慢
🧯 强制GCBOOLEANtrue强制Python垃圾回收(gc.collect),可能更彻底但会有短暂停顿
🔌 任意输入opt*任意类型直通输入:把它接在你想触发优化的环节中间

Outputs (1)

NameTypeDescription
🔌 任意输出*—