Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 43 additions & 15 deletions Doc/c-api/memory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ behavior when requesting zero bytes, are available for allocating and releasing
memory from the Python heap.

The :ref:`default memory allocator <default-memory-allocators>` uses the
:ref:`pymalloc memory allocator <pymalloc>`.
:ref:`pymalloc memory allocator <pymalloc>`. In the
:term:`free-threaded <free threading>` build, the default is the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a new free-threaded build term that you can use if you aren't planning on backporting this past 3.14.

:ref:`mimalloc memory allocator <mimalloc>` instead.

.. warning::

Expand All @@ -215,6 +217,11 @@ The :ref:`default memory allocator <default-memory-allocators>` uses the

The default allocator is now pymalloc instead of system :c:func:`malloc`.

.. versionchanged:: 3.13

In the :term:`free-threaded <free threading>` build, the default allocator
is now :ref:`mimalloc <mimalloc>`.

.. c:function:: void* PyMem_Malloc(size_t n)

Allocates *n* bytes and returns a pointer of type :c:expr:`void*` to the
Expand Down Expand Up @@ -340,7 +347,9 @@ memory from the Python heap.
the :ref:`Customize Memory Allocators <customize-memory-allocators>` section.

The :ref:`default object allocator <default-memory-allocators>` uses the
:ref:`pymalloc memory allocator <pymalloc>`.
:ref:`pymalloc memory allocator <pymalloc>`. In the
:term:`free-threaded <free threading>` build, the default is the
:ref:`mimalloc memory allocator <mimalloc>` instead.

.. warning::

Expand Down Expand Up @@ -420,23 +429,24 @@ Default Memory Allocators

Default memory allocators:

=============================== ==================== ================== ===================== ====================
Configuration Name PyMem_RawMalloc PyMem_Malloc PyObject_Malloc
=============================== ==================== ================== ===================== ====================
Release build ``"pymalloc"`` ``malloc`` ``pymalloc`` ``pymalloc``
Debug build ``"pymalloc_debug"`` ``malloc`` + debug ``pymalloc`` + debug ``pymalloc`` + debug
Release build, without pymalloc ``"malloc"`` ``malloc`` ``malloc`` ``malloc``
Debug build, without pymalloc ``"malloc_debug"`` ``malloc`` + debug ``malloc`` + debug ``malloc`` + debug
=============================== ==================== ================== ===================== ====================
=================================== ======================= ================== ===================== ====================
Configuration Name PyMem_RawMalloc PyMem_Malloc PyObject_Malloc
=================================== ======================= ================== ===================== ====================
Release build ``"pymalloc"`` ``malloc`` ``pymalloc`` ``pymalloc``
Debug build ``"pymalloc_debug"`` ``malloc`` + debug ``pymalloc`` + debug ``pymalloc`` + debug
Release build, without pymalloc ``"malloc"`` ``malloc`` ``malloc`` ``malloc``
Debug build, without pymalloc ``"malloc_debug"`` ``malloc`` + debug ``malloc`` + debug ``malloc`` + debug
Free-threaded build ``"mimalloc"`` ``malloc`` ``mimalloc`` ``mimalloc``
Free-threaded debug build ``"mimalloc_debug"`` ``malloc`` + debug ``mimalloc`` + debug ``mimalloc`` + debug
=================================== ======================= ================== ===================== ====================

Legend:

* Name: value for :envvar:`PYTHONMALLOC` environment variable.
* ``malloc``: system allocators from the standard C library, C functions:
:c:func:`malloc`, :c:func:`calloc`, :c:func:`realloc` and :c:func:`free`.
* ``pymalloc``: :ref:`pymalloc memory allocator <pymalloc>`.
* ``mimalloc``: :ref:`mimalloc memory allocator <mimalloc>`. The pymalloc
allocator will be used if mimalloc support isn't available.
* ``mimalloc``: :ref:`mimalloc memory allocator <mimalloc>`.
* "+ debug": with :ref:`debug hooks on the Python memory allocators
<pymem-debug-hooks>`.
* "Debug build": :ref:`Python build in debug mode <debug-build>`.
Expand Down Expand Up @@ -733,9 +743,27 @@ The mimalloc allocator

.. versionadded:: 3.13

Python supports the mimalloc allocator when the underlying platform support is available.
mimalloc "is a general purpose allocator with excellent performance characteristics.
Initially developed by Daan Leijen for the runtime systems of the Koka and Lean languages."
Python supports the `mimalloc <https://github.com/microsoft/mimalloc/>`__
allocator when the underlying platform support is available.
mimalloc is a general purpose allocator with excellent performance
characteristics, initially developed by Daan Leijen for the runtime systems
of the Koka and Lean languages.

Unlike :ref:`pymalloc <pymalloc>`, which is optimized for small objects (512
bytes or fewer), mimalloc handles allocations of any size.

In the :term:`free-threaded <free threading>` build, mimalloc is the default
and **required** allocator for the :c:macro:`PYMEM_DOMAIN_MEM` and
:c:macro:`PYMEM_DOMAIN_OBJ` domains. It cannot be disabled in free-threaded
builds. The free-threaded build uses per-thread mimalloc heaps, which allows
allocation and deallocation to proceed without locking in most cases.

In the default (non-free-threaded) build, mimalloc is available but not the
default allocator. It can be selected at runtime using
:envvar:`PYTHONMALLOC`\ ``=mimalloc`` (or ``mimalloc_debug`` to include
:ref:`debug hooks <pymem-debug-hooks>`). It can be disabled at build time
using the :option:`--without-mimalloc` configure option, but this option
cannot be combined with :option:`--disable-gil`.

tracemalloc C API
=================
Expand Down
20 changes: 17 additions & 3 deletions Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1074,21 +1074,35 @@ conflict.
* ``pymalloc_debug``: same as ``pymalloc`` but also install debug hooks.
* ``mimalloc_debug``: same as ``mimalloc`` but also install debug hooks.

.. note::

In the :term:`free-threaded <free threading>` build, the ``malloc``,
``malloc_debug``, ``pymalloc``, and ``pymalloc_debug`` values are not
supported. Only ``default``, ``debug``, ``mimalloc``, and
``mimalloc_debug`` are accepted.

.. versionadded:: 3.6

.. versionchanged:: 3.7
Added the ``"default"`` allocator.

.. versionchanged:: 3.13
Added the ``"mimalloc"`` and ``"mimalloc_debug"`` allocators.
In the :term:`free-threaded <free threading>` build, the ``"malloc"``,
``"malloc_debug"``, ``"pymalloc"``, and ``"pymalloc_debug"`` values are
not accepted.


.. envvar:: PYTHONMALLOCSTATS

If set to a non-empty string, Python will print statistics of the
:ref:`pymalloc memory allocator <pymalloc>` every time a new pymalloc object
arena is created, and on shutdown.
:ref:`pymalloc memory allocator <pymalloc>` or the
:ref:`mimalloc memory allocator <mimalloc>` (whichever is in use)
every time a new object arena is created, and on shutdown.

This variable is ignored if the :envvar:`PYTHONMALLOC` environment variable
is used to force the :c:func:`malloc` allocator of the C library, or if
Python is configured without ``pymalloc`` support.
Python is configured without both ``pymalloc`` and ``mimalloc`` support.

.. versionchanged:: 3.6
This variable can now also be used on Python compiled in release mode.
Expand Down
3 changes: 3 additions & 0 deletions Doc/using/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,9 @@ also be used to improve performance.
Disable the fast :ref:`mimalloc <mimalloc>` allocator
(enabled by default).

This option cannot be used together with :option:`--disable-gil`
because the :term:`free-threaded <free threading>` build requires mimalloc.

See also :envvar:`PYTHONMALLOC` environment variable.

.. option:: --without-pymalloc
Expand Down
Loading