Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

Fix issue with models staying loaded in memory.

comfyanonymousc54d3ed5e628
diff --git a/comfy/model_management.py b/comfy/model_management.py
index ca7221f36..33401b241 100644
--- a/comfy/model_management.py
+++ b/comfy/model_management.py
@@ -561,7 +561,9 @@ def loaded_models(only_currently_used=False):
def cleanup_models(keep_clone_weights_loaded=False):
to_delete = []
for i in range(len(current_loaded_models)):
- if sys.getrefcount(current_loaded_models[i].model) <= 2:
+ #TODO: very fragile function needs improvement
+ num_refs = sys.getrefcount(current_loaded_models[i].model) - current_loaded_models[i].model.lowvram_patch_counter()
+ if num_refs <= 2:
if not keep_clone_weights_loaded:
to_delete = [i] + to_delete
#TODO: find a less fragile way to do this.