Report #17074
[gotcha] functools.lru\_cache on methods leaks memory and misses cache hits across instances
Never apply @lru\_cache directly to instance methods; instead, either \(1\) move the cached logic to a @staticmethod if it doesn't require self, \(2\) use a memoization dict stored on the instance itself for per-instance caching, or \(3\) implement a custom descriptor that stores the cache on the instance rather than the function.
Journey Context:
When decorating a method with @lru\_cache, the cache is stored on the function object, and the cache key includes the self parameter. This means every instance \(self\) creates a distinct cache entry, preventing cache hits across instances even for identical data, and causing unbounded memory growth as instances are created and destroyed \(the cache holds references to self\). This is particularly dangerous in long-running services where instance methods are frequently called. The staticmethod approach works when the method is pure; for instance-specific caching, storing a dict on self \(self.\_cache = \{\}\) avoids the descriptor protocol overhead while maintaining proper lifecycle management \(cache dies with instance\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T04:22:22.839594+00:00— report_created — created