liveBook · Manning

Sys.getsizeof

As explained in Fast Python: High performance techniques for large datasets

sys.getsizeof is a Python standard-library function that returns an object's shallow memory size in bytes.

sys.getsizeof returns an object's shallow memory size in bytes. For containers such as dictionaries, iterators, and lists, that measurement excludes the objects they reference, so it can severely understate the structure's footprint. section 2.4.1

Measuring station data

A dictionary containing station data reports 240 bytes, its values view 40 bytes, and a list of those values 64 bytes. A list holding 17 entries reports 248 bytes, while its first entry contains 1,303,981 characters and reports 10,431,904 bytes. The list size excludes its contents; the character representation adds further memory. section 2.4.1

Interpret measurements by separating container overhead from referenced content:

  • Include the sizes of contained objects when estimating a structure rather than treating the container's result as its total footprint. section 2.4.1
  • Account for representation costs: entries may be pointers, and character data may be represented by larger Python objects than the underlying file bytes. section 2.4.1
  • Use a recursive implementation for many nested objects, but do not assume it captures allocations made inside low-level libraries. section 2.4.1 section 2.4.5

How Fast Python: High performance techniques for large datasets covers it

How deep it goes
Explains shallow container sizing, referenced content, recursive accounting, and low-level allocation limits.
Examples
Measures dictionary variants, station lists, file-sized strings, character objects, and expanded in-memory representations.
What it assumes
Assumes familiarity with Python containers, iterators, objects, and references while exposing their memory consequences empirically.
What you can do after
Evaluate memory estimates by separating container overhead, referenced contents, object representation, and unreported library allocations.
How it connects
Connects dictionaries, iterators, lists, pointers, Python objects, recursive sizing, and CPython memory allocation.

Also explained in

Compare how 2 books explain sys.getsizeof →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast Python: High performance techniques for large datasets. The section links point to the parts of the book it draws on.