Suppose you want to store as much data in memory as possible, but never want your program to use up all available memory. In Python we can use the weakref
module for this purpose.
With weakref
, and the WeakValueDictionary
, we can add class
instances to a dictionary without worrying about running out of memory. The values may be garbage collected at any time.
This program uses the dataclass
attribute for a simple class
, and it adds instances of the class
to either a WeakValueDictionary
, or a regular dictionary.
WeakValueDictionary
, and add 50 million instances of the StorageItem
class
to it.StorageItem
instances to it as well.weakref
can be garbage-collected, so it uses just 6.8 MB, while the dictionary causes memory usage of almost 8 GB.import weakref from dataclasses import dataclass @dataclass class StorageItem: name: str size: int use_weak = True print(use_weak) if use_weak: # Version 1: use WeakValueDictionary to allow Python to garbage collect items on low memory. w = weakref.WeakValueDictionary() for i in range(50000000): w[i] = StorageItem("Parrot", i) else: # Version 2: use dictionary, which uses memory that cannot be freed. d = {} for i in range(50000000): d[i] = StorageItem("Parrot", i)Trueweakref memory: 6.8 MB dictionary memory: 8000.0 MB
For caches, where we store data in memory for later use, weakref
and WeakValueDictionary
are ideal. We can add data to the cache without worrying about running out of memory.