Comfy-Org / Comfy-Org/ComfyUI

free_memory is not work

Open
#4,655 15 comments 0 reactions 0 assignees View on GitHub
Potential Bug
Dominant language
Python
Stars
133k
Forks
15.7k
Avg merge
1d 10h
Merged PRs (30d)
153

Description

### Expected Behavior

free_memory works fine

### Actual Behavior

something not release

### Steps to Reproduce
my version is commit 38c22e6.
This problem also exists in previous versions like [v0.0.8](https://github.com/comfyanonymous/ComfyUI/tree/v0.0.8).

1. edit `prompt_worker` function and add log to display rss.
2. set free_memory to True
3. start comfyui `main.py --cpu --disable-all-custom-nodes`
4. run a simple workflow with `CheckpointLoaderSimple`
[workflow.json](https://github.com/user-attachments/files/16782713/workflow.43.json)

```python
def prompt_worker(q, server):
e = execution.PromptExecutor(server, lru_size=args.cache_lru)
last_gc_collect = 0
need_gc = False
gc_collect_interval = 10.0
import psutil

process = psutil.Process()
while True:
timeout = 1000.0
if need_gc:
timeout = max(gc_collect_interval - (current_time - last_gc_collect), 0.0)

queue_item = q.get(timeout=timeout)
if queue_item is not None:
item, item_id = queue_item
execution_start_time = time.perf_counter()
prompt_id = item[1]
server.last_prompt_id = prompt_id
logging.info(
f"before e.execute {round(process.memory_info().rss / (1024**2), 2)}MB"
)

e.execute(item[2], prompt_id, item[3], item[4])
logging.info(
f"after e.execute {round(process.memory_info().rss / (1024**2), 2)}MB"
)
need_gc = True
q.task_done(
item_id,
e.history_result,
status=execution.PromptQueue.ExecutionStatus(
status_str="success" if e.success else "error",
completed=e.success,
messages=e.status_messages,
),
)
if server.client_id is not None:
server.send_sync(
"executing",
{"node": None, "prompt_id": prompt_id},
server.client_id,
)

current_time = time.perf_counter()
execution_time = current_time - execution_start_time
logging.info("Prompt executed in {:.2f} seconds".format(execution_time))

flags = q.get_flags()
free_memory = True # flags.get("free_memory", False)

if flags.get("unload_models", free_memory):
comfy.model_management.unload_all_models()
need_gc = True
last_gc_collect = 0

if free_memory:
e.reset()
need_gc = True
last_gc_collect = 0
logging.info(f"run e.reset()")

if need_gc:
current_time = time.perf_counter()
if (current_time - last_gc_collect) > gc_collect_interval:
comfy.model_management.cleanup_models()
gc.collect()
comfy.model_management.soft_empty_cache()
last_gc_collect = current_time
need_gc = False
logging.info(
f"after need_gc {round(process.memory_info().rss / (1024**2), 2)}MB"
)
```

### Debug Logs

```powershell
got prompt
before e.execute 350.97MB
model weight dtype torch.float32, manual cast: None
model_type EPS
Using split attention in VAE
Using split attention in VAE
Requested to load SD1ClipModel
Loading 1 new model
loaded completely 0.0 235.84423828125 True
Requested to load BaseModel
Loading 1 new model
loaded completely 0.0 3278.812271118164 True
100%|███████████████████████████████████| 1/1 [00:04<00:00, 4.08s/it]
Requested to load AutoencoderKL
Loading 1 new model
loaded completely 0.0 319.11416244506836 True
after e.execute 5324.84MB
Prompt executed in 17.68 seconds
run e.reset()
after need_gc 3316.52MB
```

### Other

1. before e.execute 350.97MB
2. after e.execute 5324.84MB
3. run e.reset()
5. after need_gc 3316.52MB

nearly 3G is not released in memory.
I'm using a 2G checkpoint, if I use a bigger one it will be bigger
what are these and how do I release them?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.