Skip to content

Commit 3b4d1b8

Browse files
perf(app): gc before every queue item
This reduces peak memory usage at a negligible cost. Queue items typically take on the order of seconds, making the time cost of a GC essentially free. Not a great idea on a hotter code path though.
1 parent c66201c commit 3b4d1b8

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

invokeai/app/services/session_processor/session_processor_default.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import gc
12
import traceback
23
from contextlib import suppress
34
from threading import BoundedSemaphore, Thread
@@ -439,6 +440,12 @@ def _process(
439440
poll_now_event.wait(self._polling_interval)
440441
continue
441442

443+
# GC-ing here can reduce peak memory usage of the invoke process by freeing allocated memory blocks.
444+
# Most queue items take seconds to execute, so the relative cost of a GC is very small.
445+
# Python will never cede allocated memory back to the OS, so anything we can do to reduce the peak
446+
# allocation is well worth it.
447+
gc.collect()
448+
442449
self._invoker.services.logger.info(
443450
f"Executing queue item {self._queue_item.item_id}, session {self._queue_item.session_id}"
444451
)

0 commit comments

Comments
 (0)