why memoizing is faster

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Mar 26 07:50:36 EDT 2011


On Sat, 26 Mar 2011 12:06:46 +0100, Andrea Crotti wrote:

> About this global caching thing I thought, but isn't this a source of
> possible HUGE memory leaks?

Hypothetically, but probably not that huge. And I wouldn't call it a 
*leak* as such, since you can access the memory and recover it if you 
want to.

The cache given is potentially unbounded in size, but in practical terms, 
it's unlikely to get that big. Say you have a few million key:value pairs 
in a dict:

>>> d = {}
>>> for i in range(3000000):
...     d[i] = 2**100 + i
...
>>> sys.getsizeof(d)
100663432
>>> sum(sys.getsizeof(k)+sys.getsizeof(v) for k,v in d.items())
125934462

That's of the order of 200 MB of memory -- not that much for today's 
systems. I've had people email me .doc files that big *wink*

But of course, if you want a more sophisticated caching system, you can 
build one. But this will trade off memory for time: the cache will be 
slower, there will be more misses, but you won't use as much memory.


> I mean, when is the object _cache freed from the memory?

When the function is freed, which will happen at program exit, or if you 
explicitly delete the function.



-- 
Steven



More information about the Python-list mailing list