Nodes/IAMCCS-nodes/Hard RAM/VRAM Purge (trim working set)
ComfyUI Node

Hard RAM/VRAM Purge (trim working set)

Kick ComfyUI's RAM hoarding out the door between long runs

By IAMCCS·Created 11 months ago·Updated 7 days ago· 113
Hard RAM/VRAM Purge (trim working set)
    • report
    • rss_before_gb
    • rss_after_gb
    • trim_applied
    unload_all_modelstrue
    soft_empty_cachetrue
    ipc_collecttrue
    trim_working_settrue
    gc_passes3
    console_logtrue

    After a long video generation, ComfyUI's Python process can be holding onto gigabytes of RAM it has no business keeping - cached tensors, IPC buffers, orphaned graphs. The next generation then starts from a bloated working set, pages, and feels like it's running through treacle. HardMemoryPurge is the sledgehammer for that moment: it unloads models, empties caches, runs the garbage collector, and on Windows even trims the process working set. You put it in the graph, run it once, and watch RSS drop.

    It's called "hard" for a reason. This is not a gentle "soft empty cache" - it unloads all loaded models by default, so anything downstream will need to reload on the next run. Use it as a reset point between big jobs, not as a regular inline node.

    How it works

    It delegates to the pack's _hard_memory_cleanup routine, which does, in order:

    1. GPU cleanup - unload_all_models unloads loaded models from VRAM; soft_empty_cache calls the empty-cache routine.
    2. IPC collection - torch.cuda.ipc_collect() to reclaim cross-process CUDA buffers (ipc_collect).
    3. GC passes - runs gc.collect() gc_passes times (default 3) to chase down Python cycles that single-collect misses.
    4. Working-set trim - trim_working_set calls the Windows APIs EmptyWorkingSet and SetProcessWorkingSetSize(-1,-1) to actually shrink the process's committed RAM. On non-Windows this step reports itself as unavailable rather than pretending.

    It measures the process RSS before and after, so you get outputs rss_before_gb, rss_after_gb, plus a report (full JSON) and trim_applied (whether the working-set trim ran). console_log mirrors the numbers to the console.

    Inputs that matter

    • unload_all_models and soft_empty_cache - both on by default. This is the full-strength version; expect reloads after.
    • trim_working_set - the Windows-only feature; harmless elsewhere.
    • gc_passes - 3 is fine; bump to 5 only if you see the report plateauing early.

    Install

    Part of IAMCCS-nodes:

    cd ComfyUI/custom_nodes
    git clone https://github.com/IAMCCS/IAMCCS-nodes.git
    

    Or ComfyUI Manager → "IAMCCS" → install → restart. No extra dependencies.

    Gotchas

    Two traps. First: because it unloads models, running it inside a generation loop is self-sabotage - you'll pay for reloads every iteration. Put it at a segment boundary or after a run, not mid-sampling. Second: the working-set trim is real and it works, but rss_after_gb reflects the committed working set, not necessarily a guarantee of headroom for the next run - if the next job still OOMs, the issue is VRAM fragmentation or genuinely insufficient RAM, not this node's failure. It's a hygiene tool, not a memory expansion pack.

    CategoryIAMCCS/HW

    Inputs (6)

    NameTypeDefaultDescription
    unload_all_modelsBOOLEANtrue
    soft_empty_cacheBOOLEANtrue
    ipc_collectBOOLEANtrue
    trim_working_setBOOLEANtrue
    gc_passesINT31–10
    console_logBOOLEANtrue

    Outputs (4)

    NameTypeDescription
    reportSTRING
    rss_before_gbFLOAT
    rss_after_gbFLOAT
    trim_appliedBOOLEAN