matthewrudy / memoist

ActiveSupport::Memoizable with a few enhancements
MIT License
920 stars 99 forks source link

Memory leaks when memoizing class methods #47

Open hgani opened 8 years ago

hgani commented 8 years ago

Hi,

I'd like to confirm my understanding about how class method memoization works in order to establish a best practice for myself and hopefully will be useful for others as well.

Here's an example ...

class Person

class << self extend Memoist def with_overdue_taxes(arg)

...

end
memoize :with_overdue_taxes

end

end

If arg is an integer that varies between 1 to 1 million, that means I am potentially keeping 1 million cached entries in memory. And this won't be garbage collected because it's referenced by the class, so the server will run out of memory quickly.

Is this right?

If so, then perhaps it's worth mentioning this in the README to help fellow programmers avoid this.

Thanks

pboling commented 8 years ago

I believe you have understood it correctly. I agree that it is worth mentioning in the README.

dogweather commented 8 years ago

I don't believe this is a memory leak. The intention of the code is to cache the results as long as the class object exists. The code succeeds.

A memory leak is when the author's intent is to free up memory, but the code isn't written to do so.

hgani commented 8 years ago

@dogweather I agree that it is not a leak from the gem's point of view, because as you said, it succeeds doing what it promises.

But this discussion is intended to remind developers to be careful when using the gem in certain situations as it can unexpectedly prevent objects from being freed forever (until the process is restarted).