ArnaoutLab / diversity

Partitioned frequency- and similarity-sensitive diversity in Python
MIT License
6 stars 1 forks source link

use of @cache on member functions could be a memory leak #41

Closed chhotii-alex closed 1 year ago

chhotii-alex commented 2 years ago

In some cases you're using @cache on member functions that perform a calculation that you would like to not have re-done if/when the results are needed again, to have the results stored in the cache. I believe that this probably creates a strong reference not only to the results of the calculation-- whose relevance does not outlive the object that did the calculation-- but also probably to the parameters themselves, which will include "self". This would (I think) keep these objects from ever being garbage-collected.

Would using @cached-property instead address this concern?

The classic OOP way to do this would be to save these calculated results that might be re-used in the object itself, i.e. add to init(): def init(self): self.__foo = None

And add a conditional to the calculation member function: def foo(self): if self.foo is None: calculate foo self.foo = foo return self.__foo

If we cache things in member variables, the garbage collector has all the information it needs to intelligently make disposal decisions.