Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leaks when memoizing class methods #47

Open
hgani opened this issue Apr 14, 2016 · 3 comments
Open

Memory leaks when memoizing class methods #47

hgani opened this issue Apr 14, 2016 · 3 comments

Comments

@hgani
Copy link

hgani commented Apr 14, 2016

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
Copy link
Contributor

pboling commented Apr 14, 2016

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

@dogweather
Copy link

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
Copy link
Author

hgani commented Jun 26, 2016

@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).

stevendaniels added a commit to stevendaniels/memoist that referenced this issue Apr 9, 2020
Closes matthewrudy#47 

Updates the readme to mention memoized class methods can lead to memory bloat.
stevendaniels added a commit to stevendaniels/memoist that referenced this issue Apr 9, 2020
Closes matthewrudy#47 

Updates the readme to mention memoized class methods can lead to memory bloat.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants