Comfy-Org / Comfy-Org/ComfyUI

Loop memory usage and lack of garbage collector

Open
#5,385 0 comments 1 reaction 0 assignees View on GitHub
Potential Bug
Dominant language
Python
Stars
133k
Forks
15.7k
Avg merge
1d 6h
Merged PRs (30d)
155

Description

### Expected Behavior

The old image should not stay in memory for every loop and once the workflow is done the memory should be cleaned

### Actual Behavior

It seems that every time it loops it will replicate the image resulting in (my workflow) 7*8 images with size 7000x5000 thats kept in memory and once the workflow is done its keeping it in memory which is 25gb ram usage

### Steps to Reproduce

I've attached a workflow that you can try, it uses some custom nodes
![workflow (6)](https://github.com/user-attachments/assets/6d895736-9faf-47ff-8366-cd7a86df6038)

[loop test.json](https://github.com/user-attachments/files/17534058/loop.test.json)

just to explain the workflow real quick, it creates a 7000x5000 image, then loops 7 times where it adds "Hello" and for each of those loops it loops 8 times adding the "World" on that row which (from my understanding) means that it keeps 7*8 = 56 versions of the 7000x5000 image in memory.

### Debug Logs

```powershell
D:\stable-diffusion\ComfyUI_windows_portable>.\python_embeded\python.exe -s ComfyUI\main.py --windows-standalone-build --listen --dont-upcast-attention --disable-auto-launch
[START] Security scan
[DONE] Security scan
## ComfyUI-Manager: installing dependencies done.
** ComfyUI startup time: 2024-10-27 14:09:39.752086
** Platform: Windows
** Python version: 3.11.9 (tags/v3.11.9:de54cf5, Apr 2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)]
** Python executable: D:\stable-diffusion\ComfyUI_windows_portable\python_embeded\python.exe
** ComfyUI Path: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI
** Log path: D:\stable-diffusion\ComfyUI_windows_portable\comfyui.log
←[34m[MaraScott] ←[92mInitialization←[0m

Prestartup times for custom nodes:
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\rgthree-comfy
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Easy-Use
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI_MaraScott_Nodes
1.4 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Manager

Total VRAM 10240 MB, total RAM 65367 MB
pytorch version: 2.5.0+cu124
Set vram state to: NORMAL_VRAM
Device: cuda:0 NVIDIA GeForce RTX 3080 : cudaMallocAsync
Using pytorch cross attention
[Prompt Server] web root: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\web
D:\stable-diffusion\ComfyUI_windows_portable\python_embeded\Lib\site-packages\kornia\feature\lightglue.py:44: FutureWarning: `torch.cuda.amp.custom_fwd(args...)` is deprecated. Please use `torch.amp.custom_fwd(args..., device_type='cuda')` instead.
@torch.cuda.amp.custom_fwd(cast_inputs=torch.float32)
Adding D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes to sys.path
←[36mEfficiency Nodes:←[0m Attempting to add Control Net options to the 'HiRes-Fix Script' Node (comfyui_controlnet_aux add-on)...←[92mSuccess!←[0m
Loaded Efficiency nodes from D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\efficiency-nodes-comfyui
Loaded ControlNetPreprocessors nodes from D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\comfyui_controlnet_aux
Loaded AdvancedControlNet nodes from D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Advanced-ControlNet
Could not find AnimateDiff nodes
Loaded IPAdapter nodes from D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI_IPAdapter_plus
Could not find VideoHelperSuite nodes
### Loading: ComfyUI-Impact-Pack (V7.5)
### Loading: ComfyUI-Impact-Pack (Subpack: V0.7)
### Loading: ComfyUI-Impact-Pack (V7.5)
Loaded ImpactPack nodes from D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Impact-Pack
[Crystools ←[0;32mINFO←[0m] Crystools version: 1.20.0
[Crystools ←[0;32mINFO←[0m] CPU: 13th Gen Intel(R) Core(TM) i5-13600KF - Arch: AMD64 - OS: Windows 10
[Crystools ←[0;32mINFO←[0m] Pynvml (Nvidia) initialized.
[Crystools ←[0;32mINFO←[0m] GPU/s:
[Crystools ←[0;32mINFO←[0m] 0) NVIDIA GeForce RTX 3080
[Crystools ←[0;32mINFO←[0m] NVIDIA Driver: 560.94
[Impact Pack] Wildcards loading done.
←[34m[ComfyUI-Easy-Use] server: ←[0mv1.2.4 ←[92mLoaded←[0m
←[34m[ComfyUI-Easy-Use] web root: ←[0mD:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Easy-Use\web_version/v2 ←[92mLoaded←[0m
### Loading: ComfyUI-Impact-Pack (V7.5)
### Loading: ComfyUI-Inspire-Pack (V1.5)
Total VRAM 10240 MB, total RAM 65367 MB
pytorch version: 2.5.0+cu124
Set vram state to: NORMAL_VRAM
Device: cuda:0 NVIDIA GeForce RTX 3080 : cudaMallocAsync
### Loading: ComfyUI-Manager (V2.51.7)
### ComfyUI Revision: 2789 [5cbb01bc] | Released on '2024-10-26'
[Impact Pack] Wildcards loading done.
------------------------------------------
←[34mComfyroll Studio v1.76 : ←[92m 175 Nodes Loaded←[0m
------------------------------------------
** For changes, please see patch notes at https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/blob/main/Patch_Notes.md
** For help, please see the wiki at https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes/wiki
------------------------------------------
[ComfyUI-Manager] default cache updated: https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main/alter-list.json
[ComfyUI-Manager] default cache updated: https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main/model-list.json
[ComfyUI-Manager] default cache updated: https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main/github-stats.json
[ComfyUI-Manager] default cache updated: https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main/extension-node-map.json
[ComfyUI-Manager] default cache updated: https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main/custom-node-list.json
# 😺dzNodes: LayerStyle -> ←[1;33mCannot import name 'guidedFilter' from 'cv2.ximgproc'
A few nodes cannot works properly, while most nodes are not affected. Please REINSTALL package 'opencv-contrib-python'.
For detail refer to ←[4mhttps://github.com/chflame163/ComfyUI_LayerStyle/issues/5←[0m←[m
[Impact Pack] Wildcards loading done.
←[34m[MaraScott] ←[92mLoaded←[0m
←[92m[tinyterraNodes] ←[32mLoaded←[0m
←[36;20m[comfy_mtb] | INFO -> loaded ←[96m83←[0m nodes successfuly←[0m
←[36;20m[comfy_mtb] | INFO -> Some nodes (5) could not be loaded. This can be ignored, but go to http://0.0.0.0,:::8188/mtb if you want more information.←[0m

←[92m[rgthree-comfy] Loaded 42 fantastic nodes. 🎉←[00m

←[34mWAS Node Suite: ←[0mOpenCV Python FFMPEG support is enabled←[0m
←[34mWAS Node Suite ←[93mWarning: ←[0m`ffmpeg_bin_path` is not set in `D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\was-node-suite-comfyui\was_suite_config.json` config file. Will attempt to use system ffmpeg binaries if available.←[0m
←[34mWAS Node Suite: ←[0mFinished.←[0m ←[32mLoaded←[0m ←[0m218←[0m ←[32mnodes successfully.←[0m

←[3m←[93m"Art is the breath of life."←[0m←[3m - Liza Donnelly←[0m

Import times for custom nodes:
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\websocket_image_save.py
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\json-parser-hapse
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\Comfyui-Simple-Json-Node
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\cg-use-everywhere
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\efficiency-nodes-comfyui
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\lora-info
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI_IPAdapter_plus
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\masquerade-nodes-comfyui
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Lora-Auto-Trigger-Words
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-List-Utils
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ControlFlowUtils
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-AutomaticCFG
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\comfyui_controlnet_aux
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\mikey_nodes
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI_UltimateSDUpscale
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Custom-Scripts
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Advanced-ControlNet
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Impact-Pack
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\rgthree-comfy
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI_essentials
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-KJNodes
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-SaveImageWithMetaData
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Inspire-Pack
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI_tinyterraNodes
0.0 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI_Comfyroll_CustomNodes
0.2 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI_MaraScott_Nodes
0.3 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Manager
0.3 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Crystools
0.6 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Easy-Use
0.6 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\comfy_mtb
0.7 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI_LayerStyle
0.7 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\comfyui-art-venture
1.1 seconds: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\was-node-suite-comfyui

Starting server

To see the GUI go to: http://0.0.0.0:8188
To see the GUI go to: http://[::]:8188
FETCH DATA from: D:\stable-diffusion\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Manager\extension-node-map.json [DONE]
got prompt
Prompt executed in 33.30 seconds
```

### Other

from my understanding the ability to make for loops is something thats been added in comfyui itself so i think (and hope) this is the right place to post this.

I've attacted some images thats shows the ram usage before and after (the memory usage stays like that until you close down comfyui)
![before](https://github.com/user-attachments/assets/6196666c-d5fd-4dee-849a-c864af303139)
![after](https://github.com/user-attachments/assets/d7a4460e-3888-4fe4-baf6-8e7579affb33)

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.