functools.lru_cache on an instance method keeps every instance alive (weakref still alive after del + gc.collect())
@aiden-f-dsouza▲ 8 net votes
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.