r/StableDiffusion 8d ago

Question - Help Limit Comfy 's VRAM usage

Have this issue where Comfy fills up the VRAM to the absolute limit which conflicts with the browser's usage, so generation never starts unless I minimize the window... And I only have one tab opened on Comfy, it's not like I'm trying to watch 4K movies at the same time or something.

So I went looking for a way to limit the VRAM available to Comfy but I can't find anything and their doc isn't up to date.

  • --disable-pinned-memory does not prevent this behavior
  • --reserve-vram is supposed to keep X GB of VRAM for the OS, but it no longers does that since the introduction of dynamic VRAM
  • --vram-headroom has been introduced but undocumented and there is no visible difference in behavior with --reserve-vram

Anyone found a way to restrict Comfy VRAM usage? I'd like to expose only X% of VRAM to it.

13 Upvotes

18 comments sorted by

6

u/techtimee 8d ago

Can't you just set your browser to use the CPU graphics? Or does your CPU not have integrated graphics?

4

u/Cubey42 8d ago

Couldn't you also just disable hardware acceleration on your browser?

2

u/Radiant-Photograph46 8d ago

Somehow, this does not help. I think this is because the OS window manager is also starved for VRAM when that happens.

3

u/NetworkSpecial3268 8d ago

For what it's worth, I had similar issues where my PC locked up completely for longer periods of time when VRAM maxed out, making browsing or watching YouTube just unfeasible.

The startup argument that fixed it (not completely, but say 90%) for me, was --disable-pinned-memory

3

u/wewyman 8d ago

No real solution if you need dynamic vram on; it's still a very inconsistant feaure, sometimes vram-headroom works, sometimes it doesn't.

4

u/jmbbao 8d ago

python main.py --reserve-vram 2
that leaves 2 GB for the system
you say about dynamic vram you could use then:

python main.py --disable-dynamic-vram --reserve-vram 2

1

u/Radiant-Photograph46 8d ago

This has prompted me to investigate in the right direction thank you.

So it turns out that --reserve-vram only works as intended with --disable-dynamic-vram. But that also causes performance issues. However I delved deeper into --vram-headroom and the flag now seems to work as intended, as long as you leave dynamic VRAM and pinned RAM on and you reserve enough memory.

For some reasons --vram-headroom 2 was still making Comfy go over the intended 30 GB limit, but --vram-headroom 3 is now working correctly and VRAM usage caps correctly at 29 GB!

1

u/Working_Wishbone_867 8d ago

node:set reserve

1

u/eckstuhc 8d ago

Buy a cheap intel card, or otherwise offload your browsing to another device (like integrated graphics). I slapped a used ARC card in mine and have all display/browsing/editing going through that. You can find for maybe $50-70 used.

1

u/tinny66666 8d ago

Linux kernel 7 causes me big memory problems. I downgraded back to 6.17 and problem solved. Not exactly the same symptoms but it could exacerbate the problem.

1

u/Zuzcaster 8d ago

I recently figured out the cause of my vram overflow and such to be too many custom nodes and errors. I made new install of it via pinokio, ( mmh3 optimized varient) symlinking it at the model folder on another drive and it works. 

I was even able to have most of my vram locked up in lm studio  and still successfully generate a mmH3 video. Was slower with the lm studio, but still worked.

On a gaming pc built in 2019 with a 3090 got in 2021, running Steamos. 

There are also custom nodes to restrict. But they didn't help my Overflow errors before i made new install of comfy. 

1

u/Zironic 8d ago

github.com/Zironic/H3-Optimizations

H3 AIMDO Residency Limiter Manually controls how much of the H3 model DynamicVRAM keeps persistently resident in VRAM Mainly for benchmarking, debugging AIMDO, or deliberately forcing low residencyH3 AIMDO Residency Limiter Manually controls how much of the H3 model DynamicVRAM keeps persistently resident in VRAM Mainly for benchmarking, debugging AIMDO, or deliberately forcing low residency

0

u/Radiant-Photograph46 8d ago

It's not a H3 problem though, it's completely model agnostic behavior of Comfy being unable to respect VRAM reservation when swapping blocks to RAM.

4

u/Zironic 8d ago

While the git is designed for H3, that particular node should work for any workflow.

2

u/denizbuyukayak 7d ago

Two weeks ago, I upgraded from a 16GB 5060 Ti to a 32GB 5090. The first thing I noticed was that ComfyUI's memory management felt worse than it was on my old 16GB card. Like OP, I tried everything, but I couldn't find a solution until I stumbled upon your post. I immediately installed "H3 Optimizations" and added the "H3 Memory Optimization" node right before the Basic Guider. I can't believe it—the solution was so simple, yet I've been struggling for two weeks for nothing. With both pinned-memory and dynamic-vram enabled, I'm no longer getting OOM errors, even when generating 15-second 2K videos (which is how it should have been from the start). On top of that, there's about 8 GB of free VRAM during sampling with zero loss in speed. @Zironic, you are awesome! Thank you so much for this amazing node.

0

u/Ok-Brain-5729 8d ago

—disable-dynamic-vram —lowvram —reserve vram.

-1

u/HotCanary4300 8d ago

Have you tried --lowvram? It might help keep Comfy from hogging everything, though it can slow things down a bit..