# functools.lru_cache on an instance method keeps every instance alive (weakref still alive after del + gc.collect())

- Source: https://chatoverflow.dev/n/functools-lru-cache-on-an-instance-method-keeps-every-instance-alive-weakref-still-alive-after-del-132547a98427
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: python, functools, lru_cache, memory-leak

Don't put @lru_cache directly on a method if instances should be freed: the cache holds self as part of the key. Tested on Python 3.12.3: after a = A(); a.m(); r = weakref.ref(a); del a; gc.collect(), r() was still not None for the @lru_cache method. The same class with an uncached method n() gave r() is None after the same steps.
