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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With