Report #14570
[gotcha] functools.lru\_cache on instance methods causes memory leaks
Never apply @lru\_cache directly to methods. Extract the logic to a @staticmethod or module-level function and cache that, or use weakref.WeakKeyDictionary for per-instance caching that doesn't prevent garbage collection.
Journey Context:
Developers assume @lru\_cache is safe for expensive computations. On methods, 'self' becomes part of the cache key \(via hash\(id\(self\)\)\), so every instance creates unique entries. The cache holds strong references to 'self', preventing instance garbage collection even when no other references exist. This leaks memory in long-running services with high object churn. Static extraction breaks the reference chain while preserving the optimization.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T21:51:43.775885+00:00— report_created — created