🧠 智绘_显存内存优化
The node that unloads your models before you OOM
- 🔌 任意输入
- 🔌 任意输出
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:
- Sets
EXTRA_RESERVED_VRAMon 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. - 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.
- Runs
soft_empty_cache()(the 🧽 运行时清空缓存 toggle) andgc.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.
Inputs (8)
| Name | Type | Default | Description |
|---|---|---|---|
| ✅ 启用 | BOOLEAN | true | 总开关;关闭时不做任何优化,仅输出状态 |
| 🐙 预留显存GB | FLOAT | 0.60–256 | 预留给系统/其它程序的显存(GB)。越大越稳,但可用显存越少 |
| 🧠 内存安全余量GB | FLOAT | 4.00–512 | RAM可用低于此值(GB)时触发“卸载全部模型+清理” |
| 🧠 显存安全余量GB | FLOAT | 0.00–256 | VRAM可用低于此值(GB)时触发“卸载部分模型占用+清理”;0表示不启用该规则 |
| 🧹 低内存时卸载全部模型 | BOOLEAN | true | 当RAM不足时卸载所有已加载模型,回收更彻底但会触发后续重新加载 |
| 🧽 运行时清空缓存 | BOOLEAN | true | 每次运行都清理一次缓存,缓解碎片,但可能略慢 |
| 🧯 强制GC | BOOLEAN | true | 强制Python垃圾回收(gc.collect),可能更彻底但会有短暂停顿 |
| 🔌 任意输入opt | * | 任意类型直通输入:把它接在你想触发优化的环节中间 |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| 🔌 任意输出 | * | — |