Weakref. 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.
Example. 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.
Result The version that uses 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
Summary. 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.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.