Report #91051
[gotcha] functools.lru\_cache on instance methods caches \`self\` causing unbounded memory growth as instances are never released
Never apply @lru\_cache directly to instance methods; instead use a staticmethod or function outside the class, or implement a custom cache keyed only by the method arguments \(excluding self\) using a WeakKeyDictionary keyed on instance ID or similar pattern that allows garbage collection of instances.
Journey Context:
When @lru\_cache decorates an instance method, the cache key includes the \`self\` object. Because lru\_cache holds strong references to its keys, the instance is kept alive forever \(or until the cache entry is evicted, which never happens with maxsize=None or if the instance is unique\). In long-running services creating many transient objects, this causes a memory leak that is hard to diagnose because the instances are "stuck" in the method cache. The standard library's lru\_cache is not aware of instance lifecycle. The correct approach is to factor the method into a standalone function that takes the necessary data fields, or use a caching scheme that weakly references the instance \(e.g., caching on \`\(id\(self\), args\)\` with a WeakKeyDictionary to track instance death\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T11:25:28.587047+00:00— report_created — created