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

This would leads to memory leak #10

Open
vaab opened this issue May 23, 2018 · 3 comments
Open

This would leads to memory leak #10

vaab opened this issue May 23, 2018 · 3 comments

Comments

@vaab
Copy link
Member

vaab commented May 23, 2018

This is copy of @TimothyZhang's stackoverflow comment

from kids.cache import cache
...
class MyClass(object):
    ...
    @cache            # <-- That's all you need to do
    @property
    def name(self):
        return 1 + 1  # supposedly expensive calculation

This would leads to memory leak.

Create an instance c of MyClass, and inspect it with objgraph.show_backrefs([c], max_depth=10), there is a ref chain from the class object MyClass to c. That is to say, c would never been released until the MyClass been released.

@vaab
Copy link
Member Author

vaab commented May 23, 2018

You need to clarify your concern. Is it about memory leaks when using cache ? Most of cache operation ARE about keeping information in memory. I can understand however that we might here tighten a little the requirements.

Can you really elaborate on your concerns here, they still don't make fully sense for me.

@TimothyZhang
Copy link

TimothyZhang commented May 24, 2018

I implemented a game server with python. I create a Player instance when a user logs in, and destroy it when the user logs out. There were some cached properties in Player , and a Player instance consumes a fairly large amout of memory.

After the server runned for some time, may be a day or so, the process got OOM. After spending hours of hard work, I found the Player instances of logged out users wrer still in memory, because they are all referenced by the Player class object. After removing @cache from those properties, the issue was solved.

I think @cache should not been used on class methods or properties, or at least use with care. Also, I tried some other cache utils, they all have the same issue.

@vaab
Copy link
Member Author

vaab commented May 24, 2018

Okay, thanks. Let me help you here. This is an important topic, and there are solutions that should work : giving you a nice caching mecanism and avoid memory leaks.

First, at first glance, it seems perfectly normal that a caching mecanism keeps cache instances in memory. This is what caching is about no ? Keeping data already computed to spit them out when you require the result of the computation again without having to do the computation.

Having said that, there are many ways to control how much of this additional data you'll want to save. By default, kids.cache is using dictionary and has no control of the memory : this is perfect for one run programs that ends quickly and when the developer knows that the number of different cached results will never get out of hand (when the cached element will be called with a reasonable and fixed amount of different values). In your configuration (a long running application), you probably have to control your memory.

This is done by using some other kind of cache stores that you can plugin as the cache store in kids.cache decorator. Please see the Cache Store section in the README.rst.

However, if you notice still some memory leaks while using a controled cache store, this is abnormal and should be reported to me.

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

No branches or pull requests

2 participants