Commit 06363710 authored by Stefan Behnel's avatar Stefan Behnel

merge branch 0.19.x into master

parents 656196c3 2388e932
...@@ -45,16 +45,36 @@ A very simple example of malloc usage is the following:: ...@@ -45,16 +45,36 @@ A very simple example of malloc usage is the following::
# return the previously allocated memory to the system # return the previously allocated memory to the system
free(my_array) free(my_array)
One important thing to remember is that blocks of memory obtained with malloc Note that the C-API functions for allocating memory on the Python heap
*must* be manually released with free when one is done with them or it won't are generally preferred over the low-level C functions above as the
be reclaimed until the python process exits. This is called a memory leak. memory they provide is actually accounted for in Python's internal
memory management system. They also have special optimisations for
smaller memory blocks, which speeds up their allocation by avoiding
costly operating system calls.
The C-API functions can be found in the ``cpython.mem`` standard
declarations file::
from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free
Their interface and usage is identical to that of the corresponding
low-level C functions.
One important thing to remember is that blocks of memory obtained with
:c:func:`malloc` or :c:func:`PyMem_Malloc` *must* be manually released
with a corresponding call to :c:func:`free` or :c:func:`PyMem_Free`
when they are no longer used (and *must* always use the matching
type of free function). Otherwise, they won't be reclaimed until the
python process exits. This is called a memory leak.
If a chuck of memory needs a larger lifetime then can be managed by a If a chuck of memory needs a larger lifetime then can be managed by a
``try..finally`` block, another helpful idiom is to tie its lifetime to a ``try..finally`` block, another helpful idiom is to tie its lifetime
Python object to leverage the Python runtime's memory management, e.g.:: to a Python object to leverage the Python runtime's memory management,
e.g.::
cdef class SomeMemory: cdef class SomeMemory:
cdef doube* data cdef double* data
def __init__(self, number): def __init__(self, number):
# allocate some memory (filled with random data) # allocate some memory (filled with random data)
...@@ -75,4 +95,4 @@ Python object to leverage the Python runtime's memory management, e.g.:: ...@@ -75,4 +95,4 @@ Python object to leverage the Python runtime's memory management, e.g.::
It should be noted that Cython has special support for (multi-dimensional) It should be noted that Cython has special support for (multi-dimensional)
arrays of simple types via NumPy and memory views which are more full featured arrays of simple types via NumPy and memory views which are more full featured
and easier to work with than pointers while still retaining the speed/static and easier to work with than pointers while still retaining the speed/static
typing benefits. typing benefits.
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment