This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients neologix, njs, pitrou, rhettinger, skrah, tim.peters, trent, vstinner, wscullin
Date 2017-10-23.17:16:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
I added _PyTraceMalloc_Track() and _PyTraceMalloc_Untrack() private functions to the C API in Python 3.6. These functions were made public in Python 3.7: renamed to PyTraceMalloc_Track() and PyTraceMalloc_Untrack(). I made this change to allow numpy to trace memory allocations, to debug memory leaks.

numpy cannot use Python memory allocators because numpy requires aligned memory blocks which are required to use CPU SIMD instructions.

Stefan Krah:
> I think many people would welcome this in scientific computing: The Arrow memory format for example recommends 64 bit alignment.

Ah, that's an interesting use case.

I created attached PR 4089 to implement PyMem_AlignedAlloc():

   void* PyMem_AlignedAlloc(size_t alignment, size_t size);
   void PyMem_AlignedFree(void *ptr);

Memory allocated by PyMem_AlignedAlloc() must be freed with PyMem_AlignedFree().

We cannot reuse PyMem_Free(). On Windows, PyMem_AlignedAlloc() is implemented with _aligned_malloc() which requires to release the memory with _aligned_free().

Raymond Hettinger:
>> Adding yet another API to allocate memory has a cost
> Please don't FUD this one to death.

Statistics (size) on my PR:

 Doc/c-api/memory.rst                              |  43 +-
 Doc/whatsnew/3.7.rst                              |   4 +
 Include/internal/mem.h                            |   6 +-
 Include/objimpl.h                                 |   2 +
 Include/pymem.h                                   |  16 +-
 .../2017-10-23-19-03-38.bpo-18835.8XEjtG.rst      |   9 +
 Modules/_testcapimodule.c                         |  83 ++-
 Modules/_tracemalloc.c                            | 131 +++-
 Objects/obmalloc.c                                | 616 +++++++++++++------
 9 files changed, 655 insertions(+), 255 deletions(-)

That's quite a big change "just to add PyMem_AlignedAlloc()" :-)
Date User Action Args
2017-10-23 17:16:53vstinnersetrecipients: + vstinner, tim.peters, rhettinger, pitrou, trent, njs, skrah, neologix, wscullin
2017-10-23 17:16:53vstinnersetmessageid: <>
2017-10-23 17:16:53vstinnerlinkissue18835 messages
2017-10-23 17:16:53vstinnercreate