Report #61279
[gotcha] functools.lru\_cache on methods causes unbounded memory leaks
Do not apply @lru\_cache to methods directly; instead use @staticmethod with explicit self passed as regular arg, use weakref.WeakKeyDictionary as a manual cache, or cache at the module level with extracted data to avoid pinning the instance in memory.
Journey Context:
The cache key includes \`self\`, so the lru\_cache holds a strong reference to the instance. Even after the instance goes out of scope in user code, it remains alive in the cache, preventing garbage collection and growing memory unboundedly. Developers often assume caches are weak-referenced by default. Alternatives like \`cachetools\` with \`@cached\` and \`key=hashkey\` avoid this, but the stdlib implementation prioritizes speed over weakref semantics.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T09:20:37.341236+00:00— report_created — created