Home
Python
weakref Example
This page was last reviewed on Apr 25, 2024.
Dot Net Perls
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.
dataclass
Version 1 We create a WeakValueDictionary, and add 50 million instances of the StorageItem class to it.
Version 2 We create a regular Python dictionary, and add 50 million StorageItem instances to it as well.
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)
True
weakref 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.
This page was last updated on Apr 25, 2024 (new).
Home
Changes
© 2007-2024 Sam Allen.