Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cython
Commits
06363710
Commit
06363710
authored
Oct 11, 2013
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Plain Diff
merge branch 0.19.x into master
parents
656196c3
2388e932
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
7 deletions
+27
-7
docs/src/tutorial/memory_allocation.rst
docs/src/tutorial/memory_allocation.rst
+27
-7
No files found.
docs/src/tutorial/memory_allocation.rst
View file @
06363710
...
@@ -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 doub
l
e* 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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment