diff --git a/comfy/model_management.py b/comfy/model_management.py index ca7221f3..33401b24 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.