[issue19859] functools.lru_cache keeps objects alive forever

Nick Coghlan report at bugs.python.org
Wed Dec 4 13:15:28 CET 2013


Nick Coghlan added the comment:

On 4 December 2013 20:15, Radomir Dopieralski <report at bugs.python.org> wrote:
> But I think it's would be still worthwhile to add a note to the lru_cache's documentation, saying something like:
>
> """
> Warning! lru_cache will keep references to all the arguments for which it keeps cached values, which prevents them from being freed from memory when there are no other references. This can lead to memory leaks when you call a function with lru_cache on a lot of short-lived objects.
> """

Umm, that's part of the operational definition of a value based cache
- it needs to keep things alive, so that if a different instance shows
up with the same value, it will still get a cache hit.

We're willing to put warnings in the docs for cases where it's easy to
inadvertently introduce a security vulnerability, but not for
situations like this where using a container inappropriately may cause
it to keep objects alive that you didn't intend to keep alive.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19859>
_______________________________________


More information about the Python-bugs-list mailing list