New submission from Radomir Dopieralski:

As most naïve "memoized" decorator implementations, lru_cache keeps references 
to all the values of arguments of the decorated function in the cache. That 
means, that if we call such a decorated function with an object as a parameter, 
that object will be kept alive in memory forever -- that is, until the program 
ends. This is an obvious waste, since when we no longer have any other 
reference to that object, we are unable to call that function with the same 
parameter ever again, so it just wastes the cache space.

This is a very common case when we decorate a method -- the first parameter is 
"self". One solution for this particular case is to use a dedicated 
"memoized_method" decorator, that stores the cache on the "self" object itself, 
so that it can be released together with the object.

A more general solution uses weakrefs where possible in the cache key, maybe 
even with an additional callback that removes the cache entry when any of its 
parameters is dead. Obviously it adds some overhead and makes the caching 
decorator even slower, but it can let us save a lot of memory, especially in 
long-running applications.

To better illustrate what I mean, here is an example of such an improved 
@memoized decorator that I wrote: 
https://review.openstack.org/#/c/54117/5/horizon/utils/memoized.py

It would be great to have an option to do something similar with lru_cache, and 
if there is an interest, I would like to work on that.

----------
components: Library (Lib)
messages: 204995
nosy: thesheep
priority: normal
severity: normal
status: open
title: functools.lru_cache keeps objects alive forever
type: resource usage
versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue19859>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to