skillfed

cymem

Manage calls to calloc/free through Cython

cymem v2.0.13 21.4M downloads/30d#1,001 on PyPI462
Permissive license MIT Active released

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 cymem

uv

uv add cymem

poetry

poetry add cymem

Installing 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

Environment :: ConsoleIntended Audience :: DevelopersIntended Audience :: Science/ResearchLicense :: OSI Approved :: MIT LicenseOperating System :: MacOS :: MacOS XOperating System :: Microsoft :: WindowsOperating System :: POSIX :: LinuxProgramming Language :: CythonProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.9Programming Language :: Python :: Free Threading :: 2 - BetaTopic :: Scientific/Engineering

Tags

cython memory managementcython calloc wrapperautomatic memory deallocation cythoncython pool allocatormanage c memory in cythoncython memory poolprevent memory leaks cython
cythonmemory-managementc-extension

More Scientific/Engineering packages