kids.cache icon indicating copy to clipboard operation
kids.cache copied to clipboard

This would leads to memory leak

Open vaab opened this issue 7 years ago • 3 comments

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 avatar May 23 '18 10:05 vaab

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.

vaab avatar May 23 '18 10:05 vaab

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.

TimothyZhang avatar May 24 '18 02:05 TimothyZhang

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.

vaab avatar May 24 '18 09:05 vaab