When to use PyMem_Malloc()?

J

Jon Perez

According to the Python docs...

"In most situations ... it is recommended to allocate memory from
the Python heap specifically because the latter is under control
of the Python memory manager. For example, this is required when the
interpreter is extended with new object types written in C. Another
reason for using the Python heap is the desire to inform the Python
memory manager about the memory needs of the extension module. Even
when the requested memory is used exclusively for internal,
highly-specific purposes, delegating all memory requests to the
Python memory manager causes the interpreter to have a more accurate
image of its memory footprint as a whole. Consequently, under certain
circumstances, the Python memory manager may or may not trigger
appropriate actions, like garbage collection, memory compaction or
other preventive procedures. Note that by using the C library
allocator as shown in the previous example, the allocated memory for
the I/O buffer escapes completely the Python memory manager."


Are there any disadvantages to using PyMem_Malloc() such that
there are times when one would prefer to use a plain malloc()?
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Jon said:
Are there any disadvantages to using PyMem_Malloc() such that
there are times when one would prefer to use a plain malloc()?

Certainly. For example, you may have source code which is used
in different projects, so changing the malloc calls may not be
feasible. Or, you may use API function of other libraries that
require you to use malloc(), as they invoke free() themselves.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top