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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.