Skip to content

Commit 858eed6

Browse files
author
Your Name
committed
Manually release memory footprint to avoid OOM that may occur after long-term use
1 parent b2c2377 commit 858eed6

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

fooocusapi/worker.py

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from fooocusapi.task_queue import QueueTask, TaskQueue, TaskOutputs
1313

1414
worker_queue: TaskQueue = None
15+
last_model_name = None
1516

1617
def process_top():
1718
import ldm_patched.modules.model_management
@@ -118,6 +119,17 @@ def yield_result(_, imgs, tasks, extension='png'):
118119

119120
try:
120121
print(f"[Task Queue] Task queue start task, job_id={async_task.job_id}")
122+
# clear memory
123+
global last_model_name
124+
125+
if last_model_name is None:
126+
last_model_name = async_task.req_param.base_model_name
127+
if last_model_name != async_task.req_param.base_model_name:
128+
model_management.cleanup_models() # key1
129+
model_management.unload_all_models()
130+
model_management.soft_empty_cache() # key2
131+
last_model_name = async_task.req_param.base_model_name
132+
121133
worker_queue.start_task(async_task.job_id)
122134

123135
execution_start_time = time.perf_counter()

0 commit comments

Comments
 (0)