1 of 36

Internals of CPython memory

Vlad Sydorenko, Senior Software Engineer, SoftServe

2 of 36

3 of 36

4 of 36

5 of 36

Agenda

  • Prerequisites
    • Learn C in 2 minutes
    • Heap vs Stack
  • Object structure
    • Why number 5 consumes 28 bytes of memory
    • Immutable object structure in Python
    • Mutable object structure
  • Low-level memory management (memory allocation)

6 of 36

Learn C in 2 minutes

Pointers

Variable, that stores some address in memory.

Example:

Structures

Structure is data type available in C that allows to combine data items of different kinds.

Example:

Macroses

A Macro is typically an abbreviated name given to a piece of code or a value.

Example:

7 of 36

Heap vs Stack

Heap

  • Used for dynamic memory allocation.
  • All next slides will relate to it.

Stack

  • Used for static memory allocation

8 of 36

Why number 5 consumes 28 bytes

Warning: all tests were ran on x64 system

9 of 36

Why nothing(!) consumes 16 bytes

10 of 36

Structure of object in Python

11 of 36

Structure of object in Python

Include/object.h

12 of 36

Structure of immutable object in Python

13 of 36

Structure of int object in Python

14 of 36

Structure of int object in Python

Include/object.h:112

Include/longintrepr.h:85

15 of 36

Interesting fact about int

16 of 36

Interesting fact about int

17 of 36

Structure of mutable object in Python

18 of 36

Structure of mutable object header in Python

Include/objimpl.h:252

19 of 36

How to see size of PyGC_HEAD

20 of 36

We need to go deeper

21 of 36

22 of 36

Python memory allocators families

PYMEM_DOMAIN_RAW PEP445

Functions:

Thread-safe

PYMEM_DOMAIN_MEM

Functions:

PYMEM_DOMAIN_OBJ

Functions:

  • PyObject_Malloc()
  • PyObject_Realloc()
  • PyObject_Calloc()
  • PyObject_Free()

23 of 36

24 of 36

25 of 36

PYMEM_DOMAIN_MEM… or not?

Objects/obmalloc.c

26 of 36

Objects/obmalloc.c

27 of 36

Objects/obmalloc.c

28 of 36

29 of 36

30 of 36

31 of 36

32 of 36

33 of 36

Pool header structure

Objects/pymalloc.h

34 of 36

Useful links

  1. http://net-informations.com/faq/net/stack-heap.htm - about difference between stack and heap
  2. https://www.youtube.com/watch?v=XGF3Qu4dUqk - “Stepping through CPython”, Larry Hastings
  3. https://www.youtube.com/watch?v=d7qEzpnkWaY - “The memory chronicles”, Kavya Joshi
  4. https://www.youtube.com/watch?v=F6u5rhUQ6dU - “Memory management in Python”, Nina Zakharenko
  5. https://docs.python.org/3/c-api/memory.html - official documentation
  6. http://www.linuxjournal.com/article/6390 - memory allocation on Linux level
  7. https://rushter.com/blog/python-memory-managment/ - one more article about memory management in Python

35 of 36

36 of 36

Thanks!