Nodes/Papcorns ComfyUI Custom Nodes/Papcorns - Memory Manager
ComfyUI Node

Papcorns - Memory Manager

Watchdog Node for Long Runs That Clear VRAM for You

By papcorns·Created about a year ago·Updated about a year ago· 0
Papcorns - Memory Manager
  • image
  • image
  • status
ram_threshold70
vram_threshold70
clear_models_on_exceedtrue
clear_cache_on_exceedtrue
model_names

ComfyUI manages VRAM better than people give it credit for - it offloads, it caches, it survives most OOMs without your help. But if you run long batch jobs or video pipelines on a small card, you know the pattern: nine generations in, the machine starts swapping, the tenth run dies with "CUDA out of memory," and you restart everything. Papcorns - Memory Manager is a watchdog you drop into the graph. It checks RAM and VRAM usage against thresholds every time it executes, and if you're over, it clears what it can before your next heavy node runs.

How it works

The node reads system RAM via psutil and VRAM via torch.cuda.mem_get_info() (with comfy.model_management.get_total_memory() for the total), then compares against ram_threshold and vram_threshold. If either is exceeded and RAM isn't critically high (above ~90%, where it refuses to act to avoid making things worse), it fires cleanup:

  • RAM: garbage-collects, runs malloc_trim via ctypes, allocates and releases 100MB blocks to nudge the OS to reclaim, and - here's the fun one - writes to /proc/sys/vm/drop_caches. That last one requires root, so on most setups it silently no-ops and the node still reports it as "cleared."
  • VRAM: torch.cuda.empty_cache() in a loop with syncs, plus the eyebrow-raising trick of setting the per-process memory fraction to 10% and back in an attempt to force PyTorch to release cached blocks.
  • Models: if clear_models_on_exceed is on, it tries a minimal direct clear, falling back to ComfyUI's own unload_all_models().

Everything it does is summarized in the status STRING output - a text report like RAM: 12.3/32.0GB (38.4%) | VRAM: 6.1/8.0GB (76.2%) followed by what it did and didn't do. Pair it with a Show Text node if you want to see that without digging through the console (it also prints the whole saga there with 🍿|MEMORY| prefixes).

The inputs that matter

  • image - required, and it's a passthrough. The node doesn't touch it; it's there so the memory check sits inline in your graph. You have to wire your workflow through it, which is awkward but workable: image in, image out, your pipeline continues from the output.
  • ram_threshold / vram_threshold - percentage (0–100, default 70) that triggers cleanup.
  • clear_models_on_exceed / clear_cache_on_exceed - which cleanup actions to allow.
  • model_names - an optional comma-separated string that's purely for the status report; it doesn't target specific models, it just echoes them back at you.

Outputs: the passthrough image and the status string.

Where it's actually useful

If you're on a 8GB card running video or long batch chains, putting this node before your heaviest sampler and setting the VRAM threshold to something like 85 means the cache gets emptied before the memory-hungry step instead of after the crash. That genuinely helps in those "dies after 9 generations" scenarios. On a well-provisioned desktop with a 4090, you don't need it - ComfyUI's own management is doing this better and more carefully, and the KB's troubleshooting notes agree that shuffling memory around costs more than most people expect.

Install

It's in the Papcorns pack:

cd ComfyUI/custom_nodes
git clone https://github.com/papcorns/Papcorns-Comfyui-Custom-Nodes
cd Papcorns-Comfyui-Custom-Nodes
pip install -r requirements.txt

Restart ComfyUI (or search "Papcorns" in Manager). Requirements include psutil, which this node needs.

The honest warning

This is a sledgehammer. The RAM-clearing routine is aggressive and theatrical (the code even labels it "AGGRESSIVE RAM clearing - last chance methods"), some of it requires root, and none of it is necessary on a healthy machine. Read it as a last-resort watchdog for small-VRAM batch work, not something to sprinkle into every workflow. If your RAM is genuinely tight, drop_caches doing nothing because you're not root is the most likely outcome - the node will still say it cleared things. Take the status string with a grain of salt and watch the console.

CategoryPapcorns🍿

Inputs (6)

NameTypeDefaultDescription
imageIMAGE
ram_thresholdINT700–100
vram_thresholdINT700–100
clear_models_on_exceedBOOLEANtrue
clear_cache_on_exceedBOOLEANtrue
model_namesSTRING

Outputs (2)

NameTypeDescription
imageIMAGE
statusSTRING