Report #60878
[gotcha] functools lru\_cache unbounded memory growth leak
Always specify a finite maxsize \(e.g., 128\) unless the key space is provably tiny and static. For single-argument methods where only identity matters, use key=id to prevent the cache from holding references to large objects indefinitely.
Journey Context:
lru\_cache stores a reference to every argument tuple and return value. With maxsize=None, the cache dict grows forever. If arguments are large objects, they are kept alive by the cache even after external references drop, causing memory exhaustion. Common mistake is using lru\_cache on functions with unbounded input domains \(e.g., user IDs\). The tradeoff is speed vs memory: maxsize=None avoids eviction overhead but leaks. A bounded size uses an LRU eviction strategy, allowing garbage collection of old entries. Using key=id reduces memory if the argument's value is large but its identity is the only semantic key.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T08:40:03.912931+00:00— report_created — created