Skip to content

mem_size

Memory size estimation utilities for arbitrary Python objects.

This module provides functions to compute the memory footprint of Python objects, including recursive traversal of containers such as dicts, lists, and objects with dict or iter attributes. Custom type-specific size functions can be registered for specialized types.

Example
mem_size.get_size([1, 2, 3])  # The list object plus its contents, in bytes.
mem_size.get_size('x' * 1000)  # Strings count their payload as a leaf.

def my_size_fn(obj):
  return 8

class MyList(list):
  pass

mem_size.register(MyList, my_size_fn)  # Custom size for a custom type.

register(otype, sizefn)

Register a custom size function for a given object type.

The registered function fully accounts for the object: get_size() returns what it reports without recursing into the object contents.

Parameters:

Name Type Description Default
otype

The object type to associate with the size function.

required
sizefn

A callable that takes an object of type otype and returns its size in bytes.

required
Source code in py_misc_utils/mem_size.py
33
34
35
36
37
38
39
40
41
42
43
44
45
def register(otype, sizefn):
  """Register a custom size function for a given object type.

  The registered function fully accounts for the object: get_size()
  returns what it reports without recursing into the object contents.

  Args:
    otype: The object type to associate with the size function.
    sizefn: A callable that takes an object of type otype and returns its
      size in bytes.
  """
  with _LOCK:
    _TYPES_SIZE[otype] = sizefn

std_sizefn(obj)

Return the size of an object using sys.getsizeof.

Parameters:

Name Type Description Default
obj

Any Python object.

required

Returns:

Type Description

The size of obj in bytes as reported by sys.getsizeof.

Source code in py_misc_utils/mem_size.py
61
62
63
64
65
66
67
68
69
70
def std_sizefn(obj):
  """Return the size of an object using sys.getsizeof.

  Args:
    obj: Any Python object.

  Returns:
    The size of obj in bytes as reported by sys.getsizeof.
  """
  return sys.getsizeof(obj)

get_size(obj)

Compute the total memory size of an object in bytes.

Recursively traverses containers and references to estimate the full memory footprint of obj. Shared objects (reached via multiple paths) are counted once, and reference cycles are tolerated.

Parameters:

Name Type Description Default
obj

Any Python object.

required

Returns:

Type Description

The estimated total memory size in bytes. Note that the contents of

strings and bytes are not traversed (they are size-aware leaf types),

and objects handled by a registered size function are not recursed

into.

Source code in py_misc_utils/mem_size.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def get_size(obj):
  """Compute the total memory size of an object in bytes.

  Recursively traverses containers and references to estimate the full
  memory footprint of obj. Shared objects (reached via multiple paths)
  are counted once, and reference cycles are tolerated.

  Args:
    obj: Any Python object.

  Returns:
    The estimated total memory size in bytes. Note that the contents of
    strings and bytes are not traversed (they are size-aware leaf types),
    and objects handled by a registered size function are not recursed
    into.
  """
  return _get_size(obj, set())