Report #7112
[gotcha] functools.lru\_cache on instance methods causes unbounded memory growth
Never apply @lru\_cache to instance methods directly. Instead, store the cache on the instance \(self.\_cache = functools.lru\_cache\(maxsize=None\)\(self.\_raw\_method\)\) or use a custom cache keyed by method arguments excluding self, or use weakref.WeakMethod to avoid pinning instances.
Journey Context:
lru\_cache stores results in a dict on the wrapper function object. When decorating a method, the wrapper is shared across all instances \(it's an attribute of the function object, not the instance\). Since 'self' is part of the cache key, every distinct instance used in a call adds an entry. These entries hold strong references to the instances, preventing garbage collection even after the instance is otherwise unreachable. This creates a memory leak proportional to the number of distinct instances ever used. The fix isolates the cache per-instance or uses weak references to allow GC.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T01:48:41.273752+00:00— report_created — created