Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memoize Python Garbage Collection

If I use a memoize decorator for example similar to that in:

https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize

Do I need to worry about running out of memory and needing to manually garbage collect? For example if I have a long running Python processess that continually memoizes, won't I need to be sure that the dict does not get too large. Do memoize decorators typically also need to do cache eviction?

Why isn't this an issue with all decorators that can hold an arbitrary amount of intermediate state?

Would using an lru_cache from functools resolve this?

like image 684
James Lam Avatar asked Oct 20 '22 11:10

James Lam


1 Answers

The memoized decorator you linked has no bound on memory usage, and does not do cache eviction. So yes, if you keep calling the function with different parameters you have to worry about running out of memory.

functools.lru_cache(n) will not store more than n calls in the cache - this is perfect to limit memory usage.

like image 88
orlp Avatar answered Oct 27 '22 19:10

orlp