Skip to content
Advertisement

Python functools lru_cache with instance methods: release object

How can I use functools.lru_cache inside classes without leaking memory?

In the following minimal example the foo instance won’t be released although going out of scope and having no referrer (other than the lru_cache).

JavaScript

But foo and hence foo.big (a BigClass) are still alive

JavaScript

That means that Foo/BigClass instances are still residing in memory. Even deleting Foo (del Foo) will not release them.

Why is lru_cache holding on to the instance at all? Doesn’t the cache use some hash and not the actual object?

What is the recommended way use lru_caches inside classes?

I know of two workarounds: Use per instance caches or make the cache ignore object (which might lead to wrong results, though)

Advertisement

Answer

This is not the cleanest solution, but it’s entirely transparent to the programmer:

JavaScript

It takes the exact same parameters as lru_cache, and works exactly the same. However it never passes self to lru_cache and instead uses a per-instance lru_cache.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement