python-peps/pep-0445.txt

683 lines
22 KiB
Plaintext
Raw Normal View History

PEP: 445
Title: Add new APIs to customize memory allocators
Version: $Revision$
Last-Modified: $Date$
Author: Victor Stinner <victor.stinner@gmail.com>
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 15-june-2013
Python-Version: 3.4
Abstract
========
2013-06-17 20:46:10 -04:00
Add new APIs to customize memory allocators.
Rationale
=========
Use cases:
2013-06-18 16:05:17 -04:00
* Application embedding Python may want to isolate Python memory from
2013-06-18 16:07:52 -04:00
the memory of the application, or may want to use a different memory
2013-06-18 16:05:17 -04:00
allocator optimized for its Python usage
* Python running on embedded devices with low memory and slow CPU.
2013-06-18 16:05:17 -04:00
A custom memory allocator may be required to use efficiently the
memory and/or to be able to use all the memory of the device.
2013-06-17 20:46:10 -04:00
* Debug tool to:
2013-06-18 16:07:52 -04:00
- track the memory usage (memory leaks)
2013-06-18 16:05:17 -04:00
- get the Python filename and line number where an object was
allocated
2013-06-17 20:46:10 -04:00
- detect buffer underflow, buffer overflow and detect misuse of Python
allocator APIs (builtin Python debug hooks)
2013-06-18 16:05:17 -04:00
- force allocation to fail to test handling of ``MemoryError``
exception
Proposal
========
2013-06-17 19:02:16 -04:00
API changes
-----------
2013-06-18 15:59:48 -04:00
* Add new GIL-free (no need to hold the GIL) memory allocator functions:
2013-06-17 19:02:16 -04:00
- ``void* PyMem_RawMalloc(size_t size)``
- ``void* PyMem_RawRealloc(void *ptr, size_t new_size)``
- ``void PyMem_RawFree(void *ptr)``
2013-06-18 16:05:17 -04:00
- the behaviour of requesting zero bytes is not defined: return *NULL*
or a distinct non-*NULL* pointer depending on the platform.
2013-06-18 15:04:34 -04:00
* Add a new ``PyMemBlockAllocator`` structure::
2013-06-18 08:14:17 -04:00
typedef struct {
2013-06-18 16:05:17 -04:00
/* user context passed as the first argument
to the 3 functions */
2013-06-18 08:14:17 -04:00
void *ctx;
2013-06-18 15:04:34 -04:00
/* allocate a memory block */
2013-06-18 08:14:17 -04:00
void* (*malloc) (void *ctx, size_t size);
2013-06-18 15:04:34 -04:00
/* allocate or resize a memory block */
2013-06-18 08:14:17 -04:00
void* (*realloc) (void *ctx, void *ptr, size_t new_size);
2013-06-18 15:04:34 -04:00
/* release a memory block */
2013-06-18 08:14:17 -04:00
void (*free) (void *ctx, void *ptr);
2013-06-18 15:04:34 -04:00
} PyMemBlockAllocator;
2013-06-18 08:14:17 -04:00
2013-06-18 16:05:17 -04:00
* Add new functions to get and set internal functions of
``PyMem_RawMalloc()``, ``PyMem_RawRealloc()`` and ``PyMem_RawFree()``:
2013-06-18 15:59:48 -04:00
- ``void PyMem_GetRawAllocator(PyMemBlockAllocator *allocator)``
- ``void PyMem_SetRawAllocator(PyMemBlockAllocator *allocator)``
2013-06-18 16:18:21 -04:00
- default allocator: ``malloc()``, ``realloc()``, ``free()``
2013-06-18 08:14:17 -04:00
2013-06-18 16:05:17 -04:00
* Add new functions to get and set internal functions of
``PyMem_Malloc()``, ``PyMem_Realloc()`` and ``PyMem_Free()``:
2013-06-18 08:14:17 -04:00
2013-06-18 15:59:48 -04:00
- ``void PyMem_GetAllocator(PyMemBlockAllocator *allocator)``
- ``void PyMem_SetAllocator(PyMemBlockAllocator *allocator)``
2013-06-18 16:05:17 -04:00
- ``malloc(ctx, 0)`` and ``realloc(ctx, ptr, 0)`` must not return
*NULL*: it would be treated as an error.
2013-06-18 16:18:21 -04:00
- default allocator: ``malloc()``, ``realloc()``, ``free()``;
``PyMem_Malloc(0)`` calls ``malloc(1)``
and ``PyMem_Realloc(NULL, 0)`` calls ``realloc(NULL, 1)``
2013-06-18 15:04:34 -04:00
2013-06-18 15:59:48 -04:00
* Add new functions to get and set internal functions of
2013-06-18 16:18:21 -04:00
``PyObject_Malloc()``, ``PyObject_Realloc()`` and
2013-06-18 16:05:17 -04:00
``PyObject_Free()``:
2013-06-18 15:04:34 -04:00
2013-06-18 15:59:48 -04:00
- ``void PyObject_GetAllocator(PyMemBlockAllocator *allocator)``
- ``void PyObject_SetAllocator(PyMemBlockAllocator *allocator)``
2013-06-18 16:05:17 -04:00
- ``malloc(ctx, 0)`` and ``realloc(ctx, ptr, 0)`` must not return
*NULL*: it would be treated as an error.
2013-06-18 16:18:21 -04:00
- default allocator: the *pymalloc* allocator
2013-06-18 15:04:34 -04:00
* Add a new ``PyMemMappingAllocator`` structure::
typedef struct {
2013-06-18 16:05:17 -04:00
/* user context passed as the first argument
to the 2 functions */
2013-06-18 15:04:34 -04:00
void *ctx;
/* allocate a memory mapping */
2013-06-18 15:59:48 -04:00
void* (*alloc) (void *ctx, size_t size);
2013-06-18 15:04:34 -04:00
/* release a memory mapping */
void (*free) (void *ctx, void *ptr, size_t size);
} PyMemMappingAllocator;
* Add a new function to get and set memory mapping allocator:
- ``void PyMem_GetMappingAllocator(PyMemMappingAllocator *allocator)``
- ``void PyMem_SetMappingAllocator(PyMemMappingAllocator *allocator)``
2013-06-18 16:05:17 -04:00
- Currently, this allocator is only used internally by *pymalloc* to
allocate arenas.
2013-06-18 08:14:17 -04:00
* Add a new function to setup the builtin Python debug hooks when memory
2013-06-17 20:46:10 -04:00
allocators are replaced:
2013-06-17 19:02:16 -04:00
- ``void PyMem_SetupDebugHooks(void)``
2013-06-18 16:18:21 -04:00
- the function does nothing is Python is compiled not compiled in
debug mode
2013-06-18 16:05:17 -04:00
* The following memory allocators always returns *NULL* if size is
2013-06-18 16:18:21 -04:00
greater than ``PY_SSIZE_T_MAX`` (check before calling the internal
function): ``PyMem_RawMalloc()``, ``PyMem_RawRealloc()``,
``PyMem_Malloc()``, ``PyMem_Realloc()``, ``PyObject_Malloc()``,
``PyObject_Realloc()``.
2013-06-18 15:59:48 -04:00
2013-06-18 16:05:17 -04:00
The builtin Python debug hooks were introduced in Python 2.3 and
implement the following checks:
2013-06-17 19:52:14 -04:00
2013-06-18 16:05:17 -04:00
* Newly allocated memory is filled with the byte ``0xCB``, freed memory
is filled with the byte ``0xDB``.
* Detect API violations, ex: ``PyObject_Free()`` called on a memory
block allocated by ``PyMem_Malloc()``
2013-06-18 15:04:34 -04:00
* Detect write before the start of the buffer (buffer underflow)
* Detect write after the end of the buffer (buffer overflow)
2013-06-18 08:14:17 -04:00
Make usage of these new APIs
----------------------------
2013-06-17 19:52:14 -04:00
2013-06-18 16:05:17 -04:00
* ``PyMem_Malloc()`` and ``PyMem_Realloc()`` always call ``malloc()``
and ``realloc()``, instead of calling ``PyObject_Malloc()`` and
2013-06-15 22:03:15 -04:00
``PyObject_Realloc()`` in debug mode
2013-06-17 20:46:10 -04:00
* ``PyObject_Malloc()`` falls back on ``PyMem_Malloc()`` instead of
2013-06-18 16:05:17 -04:00
``malloc()`` if size is greater or equal than
``SMALL_REQUEST_THRESHOLD`` (512 bytes), and ``PyObject_Realloc()``
falls back on ``PyMem_Realloc()`` instead of ``realloc()``
2013-06-17 19:52:14 -04:00
* Replace direct calls to ``malloc()`` with ``PyMem_Malloc()``, or
``PyMem_RawMalloc()`` if the GIL is not held
2013-06-18 16:05:17 -04:00
* Configure external libraries like zlib or OpenSSL to allocate memory
using ``PyMem_RawMalloc()``
2013-06-17 19:52:14 -04:00
2013-06-17 19:02:16 -04:00
Examples
========
2013-06-18 15:04:34 -04:00
Use case 1: Replace Memory Allocator, keep pymalloc
2013-06-17 19:30:05 -04:00
----------------------------------------------------
2013-06-17 19:02:16 -04:00
2013-06-18 15:59:48 -04:00
Dummy example wasting 2 bytes per allocation, and 10 bytes per arena::
2013-06-17 19:02:16 -04:00
2013-06-17 19:30:05 -04:00
#include <stdlib.h>
2013-06-17 19:02:16 -04:00
2013-06-17 19:30:05 -04:00
int alloc_padding = 2;
int arena_padding = 10;
2013-06-17 19:02:16 -04:00
2013-06-17 19:30:05 -04:00
void* my_malloc(void *ctx, size_t size)
{
int padding = *(int *)ctx;
return malloc(size + padding);
}
void* my_realloc(void *ctx, void *ptr, size_t new_size)
{
int padding = *(int *)ctx;
return realloc(ptr, new_size + padding);
}
void my_free(void *ctx, void *ptr)
{
free(ptr);
}
void* my_alloc_arena(void *ctx, size_t size)
{
int padding = *(int *)ctx;
return malloc(size + padding);
}
void my_free_arena(void *ctx, void *ptr, size_t size)
{
free(ptr);
}
2013-06-17 19:02:16 -04:00
2013-06-18 15:04:34 -04:00
void setup_custom_allocator(void)
2013-06-17 19:02:16 -04:00
{
2013-06-18 15:59:48 -04:00
PyMemBlockAllocator block;
PyMemMappingAllocator mapping;
2013-06-17 19:30:05 -04:00
2013-06-18 15:59:48 -04:00
block.ctx = &alloc_padding;
block.malloc = my_malloc;
block.realloc = my_realloc;
block.free = my_free;
2013-06-17 19:02:16 -04:00
2013-06-18 15:59:48 -04:00
PyMem_SetRawAllocator(&block);
PyMem_SetAllocator(&block);
2013-06-17 19:30:05 -04:00
2013-06-18 15:59:48 -04:00
mapping.ctx = &arena_padding;
mapping.alloc = my_alloc_arena;
mapping.free = my_free_arena;
PyMem_SetMappingAllocator(mapping);
2013-06-17 19:02:16 -04:00
PyMem_SetupDebugHooks();
}
.. warning::
2013-06-18 16:05:17 -04:00
Remove the call ``PyMem_SetRawAllocator(&alloc)`` if the new
allocator are not thread-safe.
2013-06-17 19:02:16 -04:00
2013-06-18 15:04:34 -04:00
Use case 2: Replace Memory Allocator, override pymalloc
2013-06-18 08:14:17 -04:00
--------------------------------------------------------
2013-06-17 19:02:16 -04:00
2013-06-18 16:05:17 -04:00
If your allocator is optimized for allocation of small objects (less
than 512 bytes) with a short lifetime, pymalloc can be overriden:
replace ``PyObject_Malloc()``.
2013-06-17 20:46:10 -04:00
Dummy Example wasting 2 bytes per allocation::
2013-06-17 19:02:16 -04:00
2013-06-17 19:30:05 -04:00
#include <stdlib.h>
int padding = 2;
void* my_malloc(void *ctx, size_t size)
{
int padding = *(int *)ctx;
return malloc(size + padding);
}
void* my_realloc(void *ctx, void *ptr, size_t new_size)
{
int padding = *(int *)ctx;
return realloc(ptr, new_size + padding);
}
2013-06-17 19:02:16 -04:00
2013-06-17 19:30:05 -04:00
void my_free(void *ctx, void *ptr)
{
free(ptr);
}
2013-06-17 19:02:16 -04:00
2013-06-18 15:04:34 -04:00
void setup_custom_allocator(void)
2013-06-17 19:02:16 -04:00
{
2013-06-18 15:04:34 -04:00
PyMemBlockAllocator alloc;
2013-06-17 19:30:05 -04:00
alloc.ctx = &padding;
2013-06-17 19:02:16 -04:00
alloc.malloc = my_malloc;
alloc.realloc = my_realloc;
alloc.free = my_free;
2013-06-18 15:04:34 -04:00
PyMem_SetRawAllocator(&alloc);
PyMem_SetAllocator(&alloc);
PyObject_SetAllocator(&alloc);
2013-06-17 19:30:05 -04:00
2013-06-17 19:02:16 -04:00
PyMem_SetupDebugHooks();
}
2013-06-17 19:30:05 -04:00
.. warning::
2013-06-18 16:05:17 -04:00
Remove the call ``PyMem_SetRawAllocator(&alloc)`` if the new
allocator are not thread-safe.
2013-06-17 19:30:05 -04:00
2013-06-17 19:02:16 -04:00
2013-06-17 19:30:05 -04:00
Use case 3: Setup Allocator Hooks
---------------------------------
2013-06-17 19:02:16 -04:00
2013-06-18 08:14:17 -04:00
Example to setup hooks on all memory allocators::
2013-06-17 19:02:16 -04:00
struct {
2013-06-18 15:59:48 -04:00
PyMemBlockAllocator raw;
PyMemBlockAllocator mem;
PyMemBlockAllocator obj;
2013-06-17 19:30:05 -04:00
/* ... */
2013-06-17 19:02:16 -04:00
} hook;
2013-06-17 19:30:05 -04:00
static void* hook_malloc(void *ctx, size_t size)
{
2013-06-18 15:04:34 -04:00
PyMemBlockAllocator *alloc = (PyMemBlockAllocator *)ctx;
2013-06-17 19:30:05 -04:00
/* ... */
ptr = alloc->malloc(alloc->ctx, size);
/* ... */
return ptr;
}
2013-06-17 19:02:16 -04:00
2013-06-17 19:30:05 -04:00
static void* hook_realloc(void *ctx, void *ptr, size_t new_size)
{
2013-06-18 15:04:34 -04:00
PyMemBlockAllocator *alloc = (PyMemBlockAllocator *)ctx;
2013-06-17 19:30:05 -04:00
void *ptr2;
/* ... */
ptr2 = alloc->realloc(alloc->ctx, ptr, new_size);
/* ... */
return ptr2;
}
static void hook_free(void *ctx, void *ptr)
{
2013-06-18 15:04:34 -04:00
PyMemBlockAllocator *alloc = (PyMemBlockAllocator *)ctx;
2013-06-17 19:30:05 -04:00
/* ... */
alloc->free(alloc->ctx, ptr);
/* ... */
}
void setup_hooks(void)
2013-06-17 19:02:16 -04:00
{
2013-06-18 15:04:34 -04:00
PyMemBlockAllocator alloc;
2013-06-18 08:14:17 -04:00
static int installed = 0;
2013-06-17 19:30:05 -04:00
2013-06-18 08:14:17 -04:00
if (installed)
2013-06-17 19:30:05 -04:00
return;
2013-06-18 08:14:17 -04:00
installed = 1;
2013-06-17 19:02:16 -04:00
alloc.malloc = hook_malloc;
alloc.realloc = hook_realloc;
alloc.free = hook_free;
2013-06-18 15:59:48 -04:00
PyMem_GetRawAllocator(&hook.raw);
alloc.ctx = &hook.raw;
2013-06-18 15:04:34 -04:00
PyMem_SetRawAllocator(&alloc);
2013-06-17 19:02:16 -04:00
2013-06-18 15:59:48 -04:00
PyMem_GetAllocator(&hook.mem);
alloc.ctx = &hook.mem;
2013-06-18 15:04:34 -04:00
PyMem_SetAllocator(&alloc);
2013-06-17 19:02:16 -04:00
2013-06-18 15:59:48 -04:00
PyObject_GetAllocator(&hook.obj);
alloc.ctx = &hook.obj;
2013-06-18 15:04:34 -04:00
PyObject_SetAllocator(&alloc);
2013-06-17 19:02:16 -04:00
}
2013-06-17 19:52:14 -04:00
.. warning::
2013-06-18 15:04:34 -04:00
Remove the call ``PyMem_SetRawAllocator(&alloc)`` if hooks are not
2013-06-17 19:52:14 -04:00
thread-safe.
2013-06-17 19:02:16 -04:00
.. note::
2013-06-18 16:05:17 -04:00
``PyMem_SetupDebugHooks()`` does not need to be called: Python debug
hooks are installed automatically at startup.
2013-06-17 19:02:16 -04:00
Performances
============
2013-06-18 16:05:17 -04:00
Results of the `Python benchmarks suite
<http://hg.python.org/benchmarks>`_ (-b 2n3): some tests are 1.04x
faster, some tests are 1.04 slower, significant is between 115 and -191.
I don't understand these output, but I guess that the overhead cannot be
seen with such test.
2013-06-18 16:05:17 -04:00
Results of pybench benchmark: "+0.1%" slower globally (diff between
-4.9% and +5.6%).
2013-06-18 08:14:17 -04:00
The full reports are attached to the issue #3329.
Alternatives
============
2013-06-18 08:14:17 -04:00
Only have one generic get/set function
--------------------------------------
Replace the 6 functions:
2013-06-18 15:04:34 -04:00
* ``PyMem_GetRawAllocator(PyMemBlockAllocator *allocator)``
* ``PyMem_GetAllocator(PyMemBlockAllocator *allocator)``
* ``PyObject_GetAllocator(PyMemBlockAllocator *allocator)``
* ``PyMem_SetRawAllocator(allocator)``
* ``PyMem_SetAllocator(PyMemBlockAllocator *allocator)``
* ``PyObject_SetAllocator(PyMemBlockAllocator *allocator)``
with 2 functions with an additional *domain* argument:
2013-06-18 15:59:48 -04:00
* ``int PyMem_GetBlockAllocator(int domain, PyMemBlockAllocator *allocator)``
* ``int PyMem_SetBlockAllocator(int domain, PyMemBlockAllocator *allocator)``
2013-06-18 15:04:34 -04:00
These functions return 0 on success, or -1 if the domain is unknown.
2013-06-15 22:01:00 -04:00
where domain is one of these values:
* ``PYALLOC_PYMEM``
* ``PYALLOC_PYMEM_RAW``
* ``PYALLOC_PYOBJECT``
2013-06-18 15:59:48 -04:00
Drawback: the caller has to check if the result is 0, or handle the error.
2013-06-18 15:04:34 -04:00
PyMem_Malloc() reuses PyMem_RawMalloc() by default
--------------------------------------------------
2013-06-18 16:05:17 -04:00
``PyMem_Malloc()`` should call ``PyMem_RawMalloc()`` by default. So
calling ``PyMem_SetRawAllocator()`` would also also patch
``PyMem_Malloc()`` indirectly.
2013-06-18 15:04:34 -04:00
2013-06-18 15:59:48 -04:00
.. note::
2013-06-18 15:04:34 -04:00
2013-06-18 15:59:48 -04:00
In the implementation of this PEP (issue #3329),
``PyMem_RawMalloc(0)`` calls ``malloc(0)``,
whereas ``PyMem_Malloc(0)`` calls ``malloc(1)``.
2013-06-18 08:14:17 -04:00
2013-06-17 20:46:10 -04:00
Add a new PYDEBUGMALLOC environment variable
--------------------------------------------
2013-06-18 16:05:17 -04:00
To be able to use the Python builtin debug hooks even when a custom
memory allocator replaces the default Python allocator, an environment
variable ``PYDEBUGMALLOC`` can be added to setup these debug function
hooks, instead of adding the new function ``PyMem_SetupDebugHooks()``.
If the environment variable is present, ``PyMem_SetRawAllocator()``,
``PyMem_SetAllocator()`` and ``PyObject_SetAllocator()`` will reinstall
automatically the hook on top of the new allocator.
2013-06-17 20:46:10 -04:00
2013-06-18 16:05:17 -04:00
An new environment variable would make the Python initialization even
more complex. The `PEP 432 <http://www.python.org/dev/peps/pep-0432/>`_
tries to simply the CPython startup sequence.
Use macros to get customizable allocators
-----------------------------------------
2013-06-18 16:05:17 -04:00
To have no overhead in the default configuration, customizable
allocators would be an optional feature enabled by a configuration
option or by macros.
2013-06-18 16:05:17 -04:00
Not having to recompile Python makes debug hooks easier to use in
practice. Extensions modules don't have to be recompiled with macros.
2013-06-17 21:00:17 -04:00
Pass the C filename and line number
-----------------------------------
2013-06-18 16:05:17 -04:00
Define allocator functions using macros and use ``__FILE__`` and
``__LINE__`` to get the C filename and line number of a memory
allocation.
2013-06-18 15:59:48 -04:00
Example::
typedef struct {
2013-06-18 16:05:17 -04:00
/* user context passed as the first argument
to the 3 functions */
2013-06-18 15:59:48 -04:00
void *ctx;
/* allocate a memory block */
void* (*malloc) (void *ctx, const char *filename, int lineno,
size_t size);
/* allocate or resize a memory block */
void* (*realloc) (void *ctx, const char *filename, int lineno,
void *ptr, size_t new_size);
/* release a memory block */
void (*free) (void *ctx, const char *filename, int lineno,
void *ptr);
} PyMemBlockAllocator;
2013-06-18 16:05:17 -04:00
void* _PyMem_MallocTrace(const char *filename, int lineno,
size_t size);
2013-06-18 15:59:48 -04:00
/* need also a function for the Python stable ABI */
void* PyMem_Malloc(size_t size);
#define PyMem_Malloc(size) _PyMem_MallocTrace(__FILE__, __LINE__, size)
2013-06-17 20:46:10 -04:00
Passing a filename and a line number to each allocator makes the API more
2013-06-18 08:14:17 -04:00
complex: pass 3 new arguments, instead of just a context argument, to each
allocator function. The GC allocator functions should also be patched.
2013-06-18 15:59:48 -04:00
For example, ``_PyObject_GC_Malloc()`` is used in many C functions and so
2013-06-18 08:14:17 -04:00
objects of differenet types would have the same allocation location. Such
changes add too much complexity for a little gain.
2013-06-17 20:46:10 -04:00
2013-06-18 15:59:48 -04:00
GIL-free PyMem_Malloc()
-----------------------
2013-06-18 16:05:17 -04:00
When Python is compiled in debug mode, ``PyMem_Malloc()`` calls
indirectly ``PyObject_Malloc()`` which requires the GIL to be held.
2013-06-18 15:59:48 -04:00
That's why ``PyMem_Malloc()`` must be called with the GIL held.
This PEP proposes to "fix" ``PyMem_Malloc()`` to make it always call
``malloc()``. So the "GIL must be held" restriction may be removed from
``PyMem_Malloc()``.
2013-06-17 19:52:14 -04:00
Allowing to call ``PyMem_Malloc()`` without holding the GIL might break
2013-06-18 16:05:17 -04:00
applications which setup their own allocators or allocator hooks.
Holding the GIL is convinient to develop a custom allocator: no need to
care of other threads. It is also convinient for a debug allocator hook:
Python internal objects can be safetly inspected.
2013-06-18 08:14:17 -04:00
2013-06-18 16:05:17 -04:00
Calling ``PyGILState_Ensure()`` in a memory allocator may have
unexpected behaviour, especially at Python startup and at creation of a
new Python thread state.
2013-06-17 19:52:14 -04:00
Don't add PyMem_RawMalloc()
---------------------------
2013-06-18 16:05:17 -04:00
Replace ``malloc()`` with ``PyMem_Malloc()``, but only if the GIL is
held. Otherwise, keep ``malloc()`` unchanged.
2013-06-17 19:52:14 -04:00
2013-06-18 16:05:17 -04:00
The ``PyMem_Malloc()`` is used without the GIL held in some Python
functions. For example, the ``main()`` and ``Py_Main()`` functions of
Python call ``PyMem_Malloc()`` whereas the GIL do not exist yet. In this
case, ``PyMem_Malloc()`` should be replaced with ``malloc()`` (or
2013-06-18 15:59:48 -04:00
``PyMem_RawMalloc()``).
2013-06-17 19:52:14 -04:00
2013-06-18 16:05:17 -04:00
If an hook is used to the track memory usage, the ``malloc()`` memory
will not be seen. Remaining ``malloc()`` may allocate a lot of memory
and so would be missed in reports.
2013-06-18 08:14:17 -04:00
Use existing debug tools to analyze the memory
----------------------------------------------
2013-06-18 16:05:17 -04:00
There are many existing debug tools to analyze the memory. Some
examples: `Valgrind <http://valgrind.org/>`_, `Purify
<http://ibm.com/software/awdtools/purify/>`_, `Clang AddressSanitizer
<http://code.google.com/p/address-sanitizer/>`_, `failmalloc
<http://www.nongnu.org/failmalloc/>`_, etc.
2013-06-18 08:14:17 -04:00
2013-06-18 16:05:17 -04:00
The problem is to retrieve the Python object related to a memory pointer
to read its type and/or content. Another issue is to retrieve the
location of the memory allocation: the C backtrace is usually useless
(same reasoning than macros using ``__FILE__`` and ``__LINE__``), the
Python filename and line number (or even the Python traceback) is more
useful.
2013-06-18 08:14:17 -04:00
2013-06-18 15:59:48 -04:00
Classic tools are unable to introspect Python internals to collect such
2013-06-18 16:05:17 -04:00
information. Being able to setup a hook on allocators called with the
GIL held allow to collect a lot of useful data from Python internals.
2013-06-18 15:04:34 -04:00
Add msize()
-----------
2013-06-18 16:05:17 -04:00
Add another field to ``PyMemBlockAllocator`` and
``PyMemMappingAllocator``::
2013-06-18 15:04:34 -04:00
size_t msize(void *ptr);
2013-06-18 16:05:17 -04:00
This function returns the size of a memory block or a memory mapping.
Return (size_t)-1 if the function is not implemented or if the pointer
is unknown (ex: NULL pointer).
2013-06-18 15:04:34 -04:00
On Windows, this function can be implemented using ``_msize()`` and
``VirtualQuery()``.
No context argument
-------------------
2013-06-18 16:05:17 -04:00
Simplify the signature of allocator functions, remove the context
argument:
2013-06-18 15:04:34 -04:00
* ``void* malloc(size_t size)``
* ``void* realloc(void *ptr, size_t new_size)``
* ``void free(void *ptr)``
2013-06-18 16:05:17 -04:00
It is likely for an allocator hook to be reused for
``PyMem_SetAllocator()`` and ``PyObject_SetAllocator()``, or even
``PyMem_SetRawAllocator()``, but the hook must call a different function
depending on the allocator. The context is a convenient way to reuse the
same custom allocator or hook for different Python allocators.
2013-06-18 15:04:34 -04:00
2013-06-15 21:49:29 -04:00
External libraries
==================
2013-06-18 16:05:17 -04:00
Python should try to reuse the same prototypes for allocator functions
than other libraries.
2013-06-18 15:59:48 -04:00
Libraries used by Python:
2013-06-17 21:00:17 -04:00
* OpenSSL: `CRYPTO_set_mem_functions()
<http://git.openssl.org/gitweb/?p=openssl.git;a=blob;f=crypto/mem.c;h=f7984fa958eb1edd6c61f6667f3f2b29753be662;hb=HEAD#l124>`_
to set memory management functions globally
* expat: `parserCreate()
<http://hg.python.org/cpython/file/cc27d50bd91a/Modules/expat/xmlparse.c#l724>`_
has a per-instance memory handler
2013-06-18 15:59:48 -04:00
Other libraries:
* glib: `g_mem_set_vtable()
<http://developer.gnome.org/glib/unstable/glib-Memory-Allocation.html#g-mem-set-vtable>`_
2013-06-17 21:00:17 -04:00
* libxml2: `xmlGcMemSetup() <http://xmlsoft.org/html/libxml-xmlmemory.html>`_,
global
2013-06-15 21:49:29 -04:00
2013-06-17 19:02:16 -04:00
See also the `GNU libc: Memory Allocation Hooks
<http://www.gnu.org/software/libc/manual/html_node/Hooks-for-Malloc.html>`_.
2013-06-15 21:49:29 -04:00
Memory allocators
=================
2013-06-18 16:05:17 -04:00
The C standard library provides the well known ``malloc()`` function.
Its implementation depends on the platform and of the C library. The GNU
C library uses a modified ptmalloc2, based on "Doug Lea's Malloc"
(dlmalloc). FreeBSD uses `jemalloc
<http://www.canonware.com/jemalloc/>`_. Google provides tcmalloc which
is part of `gperftools <http://code.google.com/p/gperftools/>`_.
2013-06-15 21:49:29 -04:00
``malloc()`` uses two kinds of memory: heap and memory mappings. Memory
2013-06-18 16:05:17 -04:00
mappings are usually used for large allocations (ex: larger than 256
KB), whereas the heap is used for small allocations.
On UNIX, the heap is handled by ``brk()`` and ``sbrk()`` system calls on
Linux, and it is contiguous. On Windows, the heap is handled by
``HeapAlloc()`` and may be discontiguous. Memory mappings are handled by
``mmap()`` on UNIX and ``VirtualAlloc()`` on Windows, they may be
discontiguous.
Releasing a memory mapping gives back immediatly the memory to the
system. On UNIX, heap memory is only given back to the system if it is
at the end of the heap. Otherwise, the memory will only be given back to
the system when all the memory located after the released memory are
also released.
To allocate memory in the heap, the allocator tries to reuse free space.
If there is no contiguous space big enough, the heap must be increased,
even if we have more free space than required size. This issue is
called the "memory fragmentation": the memory usage seen by the system
may be much higher than real usage. On Windows, ``HeapAlloc()`` creates
a new memory mapping with ``VirtualAlloc()`` if there is not enough free
contiguous memory.
CPython has a *pymalloc* allocator for allocations smaller than 512
bytes. This allocator is optimized for small objects with a short
lifetime. It uses memory mappings called "arenas" with a fixed size of
256 KB.
2013-06-18 15:59:48 -04:00
Other allocators:
2013-06-15 21:49:29 -04:00
2013-06-18 15:59:48 -04:00
* Windows provides a `Low-fragmentation Heap
<http://msdn.microsoft.com/en-us/library/windows/desktop/aa366750%28v=vs.85%29.aspx>`_.
2013-06-15 21:49:29 -04:00
2013-06-18 15:59:48 -04:00
* The Linux kernel uses `slab allocation
<http://en.wikipedia.org/wiki/Slab_allocation>`_.
2013-06-15 21:49:29 -04:00
2013-06-18 15:59:48 -04:00
* The glib library has a `Memory Slice API
<https://developer.gnome.org/glib/unstable/glib-Memory-Slices.html>`_:
efficient way to allocate groups of equal-sized chunks of memory
2013-06-15 21:49:29 -04:00
Links
=====
2013-06-15 21:49:29 -04:00
CPython issues related to memory allocation:
* `Issue #3329: Add new APIs to customize memory allocators
<http://bugs.python.org/issue3329>`_
2013-06-15 21:49:29 -04:00
* `Issue #13483: Use VirtualAlloc to allocate memory arenas
<http://bugs.python.org/issue13483>`_
* `Issue #16742: PyOS_Readline drops GIL and calls PyOS_StdioReadline, which
isn't thread safe <http://bugs.python.org/issue16742>`_
2013-06-18 16:05:17 -04:00
* `Issue #18203: Replace calls to malloc() with PyMem_Malloc() or
PyMem_RawMalloc() <http://bugs.python.org/issue18203>`_
* `Issue #18227: Use Python memory allocators in external libraries like
zlib or OpenSSL <http://bugs.python.org/issue18227>`_
2013-06-15 21:49:29 -04:00
Projects analyzing the memory usage of Python applications:
* `pytracemalloc
<https://pypi.python.org/pypi/pytracemalloc>`_
* `Meliae: Python Memory Usage Analyzer
<https://pypi.python.org/pypi/meliae>`_
* `Guppy-PE: umbrella package combining Heapy and GSL
<http://guppy-pe.sourceforge.net/>`_
* `PySizer (developed for Python 2.4)
<http://pysizer.8325.org/>`_