Report #62192
[gotcha] functools.lru\_cache on instance methods causes unbounded memory growth
Do not apply lru\_cache directly to methods. Instead, cache in \_\_init\_\_ via self.method = lru\_cache\(maxsize=None\)\(self.\_uncached\_method\), use a WeakKeyDictionary to map instance->cache, or move the cached logic to a standalone function that receives the instance data as hashable arguments.
Journey Context:
The cache key includes \`self\`, so every distinct instance populates a new cache entry. Since the cache holds a reference to \`self\`, the instance is never garbage collected even when the rest of the program drops it. This creates a memory leak that scales with the number of instances. Using \`self.method = lru\_cache\(...\)\(self.\_method\)\` binds the cache to the instance's lifecycle, ensuring the cache dies with the instance.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T10:52:20.848703+00:00— report_created — created