cymem
Manage calls to calloc/free through Cython
What it is and what it does
cymem is a Cython library that wraps C's calloc/free functions in a Python-managed Pool object. Instead of manually tracking and freeing allocated memory in Cython code, you attach a Pool to your Cython class and call pool.alloc() for all memory allocations. When the Pool is garbage collected (along with its parent object), all tracked memory is automatically freed—eliminating manual deallocation code and the bugs that come with it.
The library is designed for performance-critical Cython code that works with C-level structs and arrays. It supports custom allocators via WrapMalloc and WrapFree, and as of version 2.0.12, is thread-safe on CPython 3.13+ free-threaded builds. With no runtime dependencies and precompiled wheels for modern Python versions and architectures, it integrates cleanly into existing Cython projects.
Use it for:
- Building arrays of C structs in Cython without writing complex __dealloc__ methods.
- Wrapping external C libraries that use private malloc/free functions via custom allocators.
- Managing deeply nested C data structures where manual deallocation is error-prone.
- Reducing memory-leak bugs in performance-critical Cython extensions.
- Simplifying memory lifecycle in Cython classes that hold C-level pointers.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
cymem provides memory-management helpers for Cython that tie C-allocated memory to Python object lifecycles, automatically freeing memory when the object is garbage collected.
Yes, if you write Cython code that allocates C memory. cymem eliminates a common source of bugs (manual deallocation) and is actively maintained with no known vulnerabilities. Install friction is medium due to Cython and compiler requirements, but that's inherent to Cython development, not cymem itself. Not relevant for pure-Python projects.
Install
cymem on PyPI
pip
pip install cymemuv
uv add cymempoetry
poetry add cymemInstalling cymem
Before you install
Precompiled wheels available for Python 3.9–3.14 across macOS, Linux, Windows, and ARM platforms; medium install friction due to Cython dependency. Active maintenance with last commit 2026-03-27 and no known vulnerabilities.
License in practice
MIT license (permissive) allows use in commercial and proprietary projects with minimal restrictions; attribution required.
Quickstart
# Install
pip install cymem
# In a .pyx Cython file:
from cymem.cymem cimport Pool
cdef Pool mem = Pool()
cdef int* data = <int*>mem.alloc(10, sizeof(int))
# Memory is automatically freed when Pool is garbage collected
Requires Cython to be installed and a C compiler available; intended for use in .pyx files, not pure Python.
Verify before relying
- Whether the package is actively used in production by major NLP/ML libraries beyond what can be inferred from download volume.
- Performance overhead of the Pool wrapper compared to direct calloc/free calls.
- Thread-safety guarantees for Python versions prior to 3.13 free-threaded builds.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (<3.15,>=3.9) |
| Install friction | medium — platform-specific wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 273 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 21,449,015/month — #1,001 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: cymem-2.0.13-cp310-cp310-macosx_10_9_x86_64.whl; cymem-2.0.13-cp310-cp310-macosx_11_0_arm64.whl; cymem-2.0.13-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.whl; cymem-2.0.13-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl; cymem-2.0.13-cp310-cp310-musllinux_1_2_aarch64.whl; cymem-2.0.13-cp310-cp310-musllinux_1_2_x86_64.whl; cymem-2.0.13-cp310-cp310-win_amd64.whl; cymem-2.0.13-cp310-cp310-win_arm64.whl; cymem-2.0.13-cp311-cp311-macosx_10_9_x86_64.whl; cymem-2.0.13-cp311-cp311-macosx_11_0_arm64.whl; cymem-2.0.13-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.whl; cymem-2.0.13-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl; cymem-2.0.13-cp311-cp311-musllinux_1_2_aarch64.whl; cymem-2.0.13-cp311-cp311-musllinux_1_2_x86_64.whl; cymem-2.0.13-cp311-cp311-win_amd64.whl; cymem-2.0.13-cp311-cp311-win_arm64.whl; cymem-2.0.13-cp312-cp312-macosx_10_13_x86_64.whl; cymem-2.0.13-cp312-cp312-macosx_11_0_arm64.whl; cymem-2.0.13-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl; cymem-2.0.13-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Tags
More Scientific/Engineering packages
NumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
pandaspandas provides fast, flexible data structures…
permissive · top 100 on PyPI
scipyscipy provides numerical algorithms for…
permissive · top 100 on PyPI
scikit-learnscikit-learn provides a comprehensive Python…
permissive · top 1,000 on PyPI
dilldill extends Python's pickle module to…
permissive · top 1,000 on PyPI
multiprocessMultiprocess is an enhanced fork of Python's…
permissive · top 1,000 on PyPI
rmm-cu12RMM provides a common interface for customizing…
permissive · top 15,000 on PyPI
librmm-cu12librmm-cu12 provides GPU memory allocation and…
permissive · top 15,000 on PyPI
preshedPreshed provides high-performance Cython hash…
permissive · top 1,000 on PyPI
blisProvides fast BLAS-like linear algebra…
permissive · top 1,000 on PyPI
umfUMF is a library for constructing and managing…
permissive · top 15,000 on PyPI
cython-lintA linter for Cython files that checks for…
permissive · top 5,000 on PyPI
fastrlockA C-level re-entrant lock for CPython that…
permissive · top 5,000 on PyPI
omnimallocOmniMalloc solves static memory allocation for…
permissive · top 15,000 on PyPI
backports.weakrefBackports the `weakref.finalize` class from…
permissive · top 5,000 on PyPI
cysignalsProvides interrupt and signal handling…
copyleft · top 15,000 on PyPI