sys.getsizeof
A Python function that reports how many bytes an object itself occupies in memory.
In more detail
For a list it counts the list object and its array of pointers, but not the objects those pointers refer to. Watching it as a list grows shows Python over-allocating capacity in jumps rather than one slot at a time. It is the quickest way to put a number on a structure's memory cost.
Example
sys.getsizeof([]) # 56 on a typical 64-bit buildRelated terms
- space complexity — How the extra memory an algorithm needs grows with the input size, written in the same Big O notation as time.
- over-allocation — Reserving more memory than is needed right now so that future growth does not require a copy every time.
- bytes, kilobytes, megabytes — Units of data size: a kilobyte is about a thousand bytes, a megabyte about a million, a gigabyte about a billion.
Taught in
- Space, In-Place Work And The Call Stack — Algorithms & Data Structures for Working Engineers
- Amortised Analysis: Why Append Is Cheap — Algorithms & Data Structures for Working Engineers
- Arrays: Contiguous Memory And Constant-Time Access — Algorithms & Data Structures for Working Engineers