Internals of CPython memory
Vlad Sydorenko, Senior Software Engineer, SoftServe
Agenda
Learn C in 2 minutes
Structures
Structure is data type available in C that allows to combine data items of different kinds.
Heap vs Stack
Heap
Stack
Why number 5 consumes 28 bytes
Warning: all tests were ran on x64 system
Why nothing(!) consumes 16 bytes
Structure of object in Python
Structure of object in Python
Include/object.h
Structure of immutable object in Python
Structure of int object in Python
Structure of int object in Python
Include/object.h:112
Include/longintrepr.h:85
Interesting fact about int
Interesting fact about int
Structure of mutable object in Python
Structure of mutable object header in Python
Include/objimpl.h:252
How to see size of PyGC_HEAD
We need to go deeper
Python memory allocators families
PYMEM_DOMAIN_RAW PEP445
Functions:
PYMEM_DOMAIN_OBJ
Functions:
PYMEM_DOMAIN_MEM… or not?
Objects/obmalloc.c
Objects/obmalloc.c
Objects/obmalloc.c
Pool header structure
Objects/pymalloc.h
Useful links
Thanks!