Report #4146
[gotcha] Memory leak when using functools.lru\_cache on instance methods
Avoid decorating methods directly with lru\_cache; instead cache on the instance dict, use a global function that takes the instance ID, or use weakref to avoid retaining self.
Journey Context:
lru\_cache stores the function arguments in a hash map. When decorating a method, 'self' is part of the cache key. This prevents the instance from being garbage collected until the cache clears or the process ends. Developers often assume the cache only stores the method's logic or primitive arguments, not the instance reference, leading to silent memory accumulation in long-running services.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T18:53:27.796356+00:00— report_created — created