Python Glossary Русский

all Python 3sysfunction

sys.getsizeof()

memory

Returns the size of an object in bytes (not counting the objects nested in it). It uses __sizeof__ plus the GC bookkeeping.

Syntax

sys.getsizeof(object, default=...) -> int

For containers only the container itself is measured: a list of a million strings reports a few megabytes of pointer array and ignores the strings. Summing sizes recursively by hand is misleading too — a shared or interned object gets counted more than once. When the real question is how much memory your code actually uses, reach for tracemalloc instead.

Examples

import sys
print(sys.getsizeof(0))  # → 28
print(sys.getsizeof([]))  # → 56
print(sys.getsizeof(''))  # → ? зависит от версии Python, на 3.14 — 41
print(sys.getsizeof({'a': 1}))  # → ? зависит от версии, на 3.14 — 184
print(sys.getsizeof(list(range(100))))  # → ? на 3.14 — 856: сам список, без элементов

See also

Open in the full glossarydocs.python.orgReport an inaccuracy