cymem
A Cython memory pool for RAII-style, garbage-collector-tied memory management
Repository Health
Technical Analysis
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 collectedcymem.Address— a helper for a single tracked allocation outside a full Pool- RAII-style memory safety for Cython
cdefclasses 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.