cymem

A Cython memory pool for RAII-style, garbage-collector-tied memory management

Library
PyPI
v2.0.13
462stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
42/100Fair
Development Activity8
Maintenance20
Community60
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
73/100Good
Architecture75
Code Quality78
Innovation70
Learning Curve70

cymem provides two small memory-management helpers for Cython, most notably cymem.Pool, a thin wrapper around calloc that ties raw C memory allocations to a Python object’s lifecycle. Instead of manually tracking and freeing every malloc’d struct in Cython code, you attach a Pool to a cdef’d class and let it record every allocation; when the owning object is garbage collected, Pool frees everything it tracked automatically, RAII-style.

What You Get

  • cymem.Pool — a calloc-backed allocator that frees all tracked memory when the Pool is garbage collected
  • cymem.Address — a helper for a single tracked allocation outside a full Pool
  • RAII-style memory safety for Cython cdef classes without writing custom __dealloc__ logic
  • Zero Python-level runtime overhead — the pool bookkeeping happens in compiled Cython code

Common Use Cases

  • Managing memory for arrays of C structs in performance-critical Cython extensions
  • Avoiding manual free() calls scattered across complex deallocation paths for nested structs
  • Underpinning spaCy and other Cython-heavy NLP libraries that need fast, safe C-level memory
  • Prototyping C-level data structures in Cython without hand-rolling a custom allocator

Under The Hood

Architecture: the entire library is a single Cython source file (cymem/cymem.pyx) compiled to a C extension; Pool maintains an internal list of allocated addresses and their sizes, exposing alloc()/realloc()/free() methods that both perform the C-level operation and update that bookkeeping list, with a __dealloc__ method that walks the list and frees everything when the Pool object is collected. Tech Stack: pure Cython/C, built via setup.py with Cython as a required build-time dependency; ships prebuilt wheels via the explosion/wheelwright release pipeline so end users don’t need a Cython toolchain to install it. Code Quality: a small, focused tests/ directory under cymem/tests/ verifies allocation/free/realloc correctness and garbage-collection-triggered cleanup; the codebase is small enough (a few hundred lines) that its scope is easy to reason about end to end. API Design: the API is deliberately minimal — Pool(), .alloc(), .realloc(), .free() — mirroring C’s calloc/realloc/free so C/Cython developers need almost no new mental model, at the cost of being a Cython-only tool inaccessible from pure Python code.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search