--no-cache-models startup flag
Feature Idea
A launch argument to make Comfy discard models after inference instead of caching them back to RAM.
Right now, the way model loading is handled goes: load to RAM -> load to VRAM -> infer -> load back and cache to RAM when VRAM is needed for other things
To provide an example usecase, Wan 2.2 HIGH and LOW noise models both get cached in RAM after they have been used, even though the workflow no longer needs the HIGH model during inference with LOW. This causes RAM to fill up quite a bit, potentially causing system hangs in some cases.
While flags such as --cache-none exists, it makes Comfy not cache anything, including models or execution data, but models are typically the things most likely to cause RAM capacity issues so the ability to not cache them would be beneficial while preserving execution cache so that every node doesn't need reprocessing each time.
Existing Solutions
No response
Other
No response
+1
Wouldn't windows just use a pagefile anyway ? I mean worst case it doesn't speed up anything (cause the ram stuff is on disk swap/pagefile)
Sure the high model is not needed in ram when you use the low model afterward, but most people run the same workflow a few times. A big pagefile/swap and letting your OS manage what is being most used is best
When creating a WAN video, upscaling in the same workflow always results in memory errors. However, upscaling alone is much more efficient. Why is that?
in same Workflow sampler>decode>upscale=error sampler>decode>save video>load video>upscale=error
Another Workflow load video>upscale=so easy
At the very least, I think there should be an option to cap sysmem usage so that things don't write to the page file. If it detects that a model or operation is requesting more than the set limit, it will purge the oldest model from system memory, before loading the current model. If purging the oldest model doesn't work, then it will purge the next oldest, try again, and so on, until it has enough room. Obviously, the estimation math might get a little tricky though.
Wouldn't windows just use a pagefile anyway ? I mean worst case it doesn't speed up anything (cause the ram stuff is on disk swap/pagefile)
Sure the high model is not needed in ram when you use the low model afterward, but most people run the same workflow a few times. A big pagefile/swap and letting your OS manage what is being most used is best
Using a pagefile is really inefficient if I'm loading the models from the same SSD that has my swap. When the RAM cache exceeds system RAM that means I'm loading models from the SSD, temporarily saving them to the same SSD, and then loading them again. Not only is this slightly slower, it wastes SSD writes.
Well regardless, memory pressure is getting rougher and rougher as these larger and larger models come out. When loading big models that need 20-30+ GB of RAM to initialize or patch, Torch still has to allocate memory and that memory shows up as committed in the task manager. That doesn't always mean that it's writing to the page file, if nothing has touched the allocated space yet, but it can result in doing so eventually. Most people are running 16-32GB RAM, with only around 5% having more than 32GB (based on the Steam Hardware Survey). So ideally, any memory optimizations should sort of try to target those specs.
All that being said, I'm not 100% of all the nuances of PyTorch and potential memory allocation optimizations, that don't completely decimate performance. It would just be nice to see a little more flexibility to cope with memory pressure, that don't result in constantly wearing SSDs down.
Wouldn't windows just use a pagefile anyway ? I mean worst case it doesn't speed up anything (cause the ram stuff is on disk swap/pagefile) Sure the high model is not needed in ram when you use the low model afterward, but most people run the same workflow a few times. A big pagefile/swap and letting your OS manage what is being most used is best
Using a pagefile is really inefficient if I'm loading the models from the same SSD that has my swap. When the RAM cache exceeds system RAM that means I'm loading models from the SSD, temporarily saving them to the same SSD, and then loading them again. Not only is this slightly slower, it wastes SSD writes.
Exactly so. I have 64 GB RAM + 24 GB VRAM. Since WAN2.2 came, in combination with complex multi-stage workflows, I run out of RAM quickly, because Comfy caches all models and Loras in RAM. Then gigabytes of data is written to pagefile to free up RAM again which is not good for SSDs as all know. Beside, I have all models stored on a super fast M2SSD with PCIe4 which would reload the models the VRAM much quicker than from the pagefile located on boot drive, which is a SATA SSD.
So in short, like OP wrote, would we need an option to prevent caching of MODELS only, or a intelligent model purge/unload from RAM and/or VRAM which are not currently in use, with a definable limit of RAM to be bused by Comfy.
"--cache-none" does not work with Loop-start - Loop-End workflows, they produce errors with this flag enabled, unfortunately.
Edit: typos fixed and description improved .
--cache-none sort-of works around this, but it causes a new problem: models are always unloaded between requests. If all you do is two-stage WAN 2.2 workflows that's fine, but if you switch between WAN 2.2 and Flux or Qwen image gen, you probably do want models to stay loaded between requests for those. I end up restarting Comfy all the time to turn --cache-none on and off, since image generation takes twice as long from it reloading models over and over.
I don't think "--no-cache-models" is right either, since it'd have the same problem.
The --cache-none behavior seems more like an accidental side-effect. It seems like it's only meant to disable caching node outputs between requests, like caching conditioning if you request the same prompt repeatedly. Causing models to be unloaded more aggressively seems unintentional. It happens because the cache itself is the only thing keeping a strong ref to the model, so disabling it causes models to get unloaded much more quickly.
I thought calling /free might help, but it doesn't. That'll free models between requests, but the problem is that both Wan models are in memory simultaneously during the same request, and it can't do anything about that.
A general solution could be a "maximum RAM" setting. If loading a model (or swapping one back to CPU) would raise the CPU memory beyond it, unload an idle model if there is one. That way, loading the second Wan model for the second pass would unload the first Wan model. But (unlike --cache-none or --no-cache-models) it wouldn't cause models to be reloaded on every request when it's not necessary.
Some have recommended leaning on page / swap... Correct me but isn't the benefit of keeping a model in system RAM to avoid limited IO bandwidth?
How is writing a second 'pagefile' copy to disk, and then later reloading that better than just reloading the original from the filesystem on the same disk?
It seems to me allowing a model to page to disk is the most pessimal use of swap and simply doubles IO usage for no benefit
Correct me but isn't the benefit of keeping a model in system RAM to avoid limited IO bandwidth?
Normally, yes, but when working with big models like WAN that need a text encoder and two large diffusion models, you are pretty much guaranteed to always exceed 32gb of system memory during a workflow execution. When this happens, Windows will then move chunks of system memory to the pagefile which is painfully slow and puts serious wear on your SSDs.
Then, when you need to run the workflow again, it will hit that point where it needs to now load from the previously pagefiled memory and so you have to wait for it to shuffle stuff currently in memory to the pagefile, to make room in sysmem, while loading the previously stored data from that same pagefile. You see where I'm going with this?
This is why people want an option like this --no-cache-models or maybe some toggle option on the model loaders. One of the big hurdles though is that currently, you can't unload models during a workflow excecution outside of using the --cache-none flag. This option works, but since there is no caching at all, nothing will persist between executions. So for example, even if nothing in a prompt changed, it will still always have to reload the text encoder and encode the prompt.
Some have recommended leaning on page / swap... Correct me but isn't the benefit of keeping a model in system RAM to avoid limited IO bandwidth?
How is writing a second 'pagefile' copy to disk, and then later reloading that better than just reloading the original from the filesystem on the same disk?
It seems to me allowing a model to page to disk is the most pessimal use of swap and simply doubles IO usage for no benefit
It makes no sense to me either. Furthermore, in my use case there are two issues with leaning on the pagefile:
-
My models are all stored on my fastest SSD, but in order to preserve the lifespan of my SSDs I have the pagefile on a spinning disk. Therefore, reloading from pagefile is slower than reloading from the SSD.
-
When getting close to RAM exhaustion, as happens with WAN 2.2 thanks to models seemingly fully being unloaded to RAM, Windows tends to start kicking other things to the pagefile too. This degrades overall system performance as other apps get paged in and out.
+1
Would it be possible to add a setting like “Cache up to X models”, where only a limited number of models are kept in memory at once, and when the limit is reached, the system automatically unloads the least recently used model and replaces it with the newly requested one?
I’m aware that cache-lru exists, but it applies at the node level rather than specifically to model handling, which makes it less effective for controlling VRAM/RAM usage when frequently switching between large models.
A model-focused LRU cache would give users more predictable memory control, reduce OOM errors, and improve workflow efficiency when working with multiple checkpoints, LoRAs, or diffusion models in a single session.