espnet2.utils.sized_dict.get_size
Less than 1 minute
espnet2.utils.sized_dict.get_size
espnet2.utils.sized_dict.get_size(obj, seen=None)
Recursively finds the size of objects in bytes.
This function calculates the total memory footprint of an object by recursively traversing through its contents. It accounts for various object types such as dictionaries, lists, sets, tuples, and custom objects with __dict__.
This implementation is based on the code found at: https://github.com/bosswissam/pysize
- Parameters:
- obj – The object whose size is to be calculated. It can be of any type.
- seen – A set of object IDs that have already been encountered during recursion. This is used to prevent infinite loops in case of self-referential objects.
- Returns: The total size of the object in bytes.
- Return type: int
Examples
>>> get_size([1, 2, 3])
80 # The size may vary based on the system and Python version.
>>> get_size({'a': 1, 'b': [2, 3]})
200 # The size may vary based on the system and Python version.
- Raises:TypeError – If the object cannot be sized.