Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3799276
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:42:25+00:00 2026-05-19T13:42:25+00:00

I’m reading the documentation for Memory Management in Python C extensions , and as

  • 0

I’m reading the documentation for Memory Management in Python C extensions, and as far as I can tell, there doesn’t really seem to be much reason to use malloc rather than PyMem_Malloc. Say I want to allocate an array that isn’t to be exposed to Python source code and will be stored in an object that will be garbage collected. Is there any reason to use malloc?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:42:26+00:00Added an answer on May 19, 2026 at 1:42 pm

    EDIT: Mixed PyMem_Malloc and PyObject_Malloc corrections; they are two different calls.

    Without the PYMALLOC_DEBUG macro activated, PyMem_Malloc is an alias of libc’s malloc(), having one special case: calling PyMem_Malloc to allocate zero bytes will return a non-NULL pointer, while malloc(zero_bytes) might return a NULL value or raise a system error (source code reference):

    /* malloc. Note that nbytes==0 tries
    to return a non-NULL pointer, distinct

    • from all other currently live pointers. This may not be possible.
      */

    Also, there is an advisory note on the pymem.h header file:

    Never mix calls to PyMem_ with
    calls to the platform malloc/realloc/
    calloc/free. For example, on Windows
    different DLLs may end up using
    different heaps, and if you use
    PyMem_Malloc you’ll get the memory
    from the heap used by the Python
    DLL; it could be a disaster if you
    free()’ed that directly in your own
    extension. Using PyMem_Free instead
    ensures Python can return the
    memory to the proper heap. As another
    example, in PYMALLOC_DEBUG mode,
    Python wraps all calls to all PyMem_
    and PyObject_ memory functions in
    special debugging wrappers that add
    additional debugging info to
    dynamic memory blocks. The system
    routines have no idea what to do
    with that stuff, and the Python
    wrappers have no idea what to do
    with raw blocks obtained directly by
    the system routines then.

    Then, there are some Python specific tunings inside PyMem_Malloc PyObject_Malloc, a function used not only for C extensions but for all the dynamic allocations while running a Python program, like 100*234, str(100) or 10 + 4j:

    >>> id(10 + 4j)
    139721697591440
    >>> id(10 + 4j)
    139721697591504
    >>> id(10 + 4j)
    139721697591440
    

    The previous complex() instances are small objects allocated on a dedicated pool.

    Small objects (<256 bytes) allocation with PyMem_Malloc PyObject_Malloc is quite efficient since it’s done from a pool 8 bytes aligned blocks, existing one pool for each block size. There are also Pages and Arenas blocks for bigger allocations.

    This comment on the source code explains how the PyObject_Malloc call is optimized:

    /*
     * The basic blocks are ordered by decreasing execution frequency,
     * which minimizes the number of jumps in the most common cases,
     * improves branching prediction and instruction scheduling (small
     * block allocations typically result in a couple of instructions).
     * Unless the optimizer reorders everything, being too smart...
     */
    

    Pools, Pages and Arenas are optimizations intended to reduce external memory fragmentation of long running Python programs.

    Check out the source code for the full detailed documentation on Python’s memory internals.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.