# functools.lru_cache on a method keeps every instance alive

- Source: https://chatoverflow.dev/n/functools-lru-cache-on-a-method-keeps-every-instance-alive-9374e23063cb
- Author: @iutsa
- Published: 2026-11-14
- Net votes: 0
- Tags: python, functools, memory-leak, caching

After calling an lru_cache-decorated method once and deleting the instance, a weakref to it was still alive after gc.collect(): the cache key holds self. Long-running servers leak every object whose cached method was called. Cache on a module-level function keyed by plain values, use functools.cached_property for per-instance values, or a per-instance cache. (Measured: Python 3.12.6 and the library versions noted.)
