{"id":978,"library":"cymem","title":"cymem","description":"cymem is a Python library that provides efficient memory-management helpers for Cython. It simplifies tying C-level memory allocations (via `calloc`/`free`) to the lifecycle of Python objects, automatically freeing memory when the owning Python object is garbage collected. The core component is `cymem.Pool`, a thin wrapper around `calloc`. Currently at version 2.0.13, it maintains a regular release cadence, often aligning with new Python version support and performance enhancements like free-threading.","status":"active","version":"2.0.13","language":"python","source_language":"en","source_url":"https://github.com/explosion/cymem","tags":["Cython","memory management","low-level","C extensions","ffi"],"install":[{"cmd":"pip install cymem","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Required for compiling and using `cymem` in Cython projects, as `cymem` itself is a Cython library that interacts with C-level memory.","package":"Cython","optional":false}],"imports":[{"note":"Used in Cython (.pyx) files to import the memory pool class. This is a cimport, not a regular Python import.","symbol":"Pool","correct":"from cymem.cymem cimport Pool"}],"quickstart":{"code":"from cymem.cymem cimport Pool\nfrom libc.stdlib cimport sizeof\n\ndef main():\n    cdef Pool mem = Pool()\n    cdef int* data1 = <int*>mem.alloc(10, sizeof(int))\n    cdef float* data2 = <float*>mem.alloc(12, sizeof(float))\n\n    # Use data1 and data2\n    data1[0] = 100\n    data2[0] = 3.14\n\n    print(f\"Data1 at index 0: {data1[0]}\")\n    print(f\"Data2 at index 0: {data2[0]}\")\n\n    # Memory is automatically freed when 'mem' (the Pool object) is garbage collected.\n    # No explicit free() calls are needed for memory allocated via Pool.\n\n# To run this, you would typically compile it with Cython:\n# cython -3 --inplace your_module.pyx\n# Then import and call main() from Python:\n# import your_module\n# your_module.main()","lang":"cython","description":"This example demonstrates how to allocate C-level memory using `cymem.Pool` within a Cython `.pyx` file. The `Pool` object handles the deallocation of all memory allocated through it when the `Pool` instance itself is garbage collected, simplifying memory management in Cython extensions."},"warnings":[{"fix":"Ensure critical sections are used for internal state access, and apply fine-grained locks or other synchronization primitives when sharing access to memory contents across threads. Do not rely on coarse-grained locks on the `Pool` instance itself for memory content synchronization.","message":"When `cymem.Pool` is used with CPython 3.13+ free-threaded builds (PEP 703), operations like `alloc()`, `free()`, and `realloc()` are thread-safe. However, reading the internal state (e.g., `addresses` dict) without explicit critical sections is not thread-safe. Users are also responsible for synchronizing access to the *contents* of the allocated memory across threads.","severity":"gotcha","affected_versions":">=2.0.12"},{"fix":"Ensure your installed Cython version is compatible with the `cymem` version. If encountering this error, try recompiling your Cython project (if you're compiling `cymem` from source or an older wheel) or updating `cymem` to a version with wheels pre-built against a compatible Cython release. It's recommended to update `pip`, `setuptools`, and `wheel` before installing to ensure the latest compatible binary wheels are used.","message":"Incompatibility between `cymem`'s compiled C++ files and the installed Cython version can lead to errors like `ValueError: cymem.cymem.Pool has the wrong size, try recompiling.` This often happens when Cython's internal structures change.","severity":"breaking","affected_versions":"<2.0.5 (older versions were more susceptible, but can still occur with mismatched builds)."},{"fix":"Carefully manage the lifetime of your `cdef` Python objects that hold `cymem.Pool` instances. Ensure that any C pointers derived from the pool are only accessed while the owning Python object and its associated `Pool` are still alive and in scope.","message":"`cymem.Pool` simplifies memory deallocation by tying it to the Python object's lifecycle. However, users must still ensure that no raw C pointers obtained from the pool outlive the Python object that owns the `Pool` instance. If the `Pool` object is garbage collected, all its managed memory is freed, invalidating any lingering pointers.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If you are developing a Cython extension, ensure your source file is named `.pyx` and is compiled with Cython. If you intend to use `cymem` from Python, you typically import a *compiled* module that uses `cymem` internally, or `cymem`'s public Python API if available, using standard Python `import` statements. Do not use `cimport` in `.py` files.","message":"Attempting to use Cython-specific `cimport` syntax (e.g., `from cymem.cymem cimport Pool`) directly in a `.py` file executed by a standard Python interpreter will result in a `SyntaxError`. Cython's `cimport` is exclusively for defining types and functions within Cython source files (`.pyx`) that will be compiled, not for Python scripts.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use `cimport` exclusively within Cython (`.pyx`) files. To use `cymem.Pool` from a Python (`.py`) file, import it like a regular Python object (e.g., `from cymem.cymem import Pool`) after the `cymem` library (or your own Cython code that `cimports` `cymem`) has been properly compiled and installed.","message":"Attempting to use Cython's `cimport` statement directly in a standard Python (`.py`) file will result in a `SyntaxError`. `cimport` is a Cython-specific keyword valid only in Cython (`.pyx`) source files, which must be compiled into a Python-loadable extension before they can be imported into Python.","severity":"breaking","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-05-12T22:04:31.873Z","next_check":"2026-06-27T00:00:00.000Z","problems":[{"fix":"Ensure `cymem` is installed using `pip install cymem`. If using a virtual environment, activate it first. If still failing, check `pip list` to confirm its presence and verify your Python path.","cause":"The `cymem` package is not installed in the current Python environment or the Python interpreter cannot find it.","error":"ModuleNotFoundError: No module named 'cymem'"},{"fix":"Reinstall `cymem` and its dependent libraries (like spaCy or thinc) from scratch, often by uninstalling and then reinstalling with `pip install --no-cache-dir --force-reinstall cymem` and similarly for any dependent packages.","cause":"This error occurs when `cymem` (or a library depending on it) was compiled against a different version of Python, Cython, or operating system architecture than the one currently being used, leading to a binary incompatibility.","error":"ValueError: cymem.cymem.Pool has the wrong size, try recompiling."},{"fix":"Uninstall and then reinstall `cymem` and its main dependent packages (e.g., `pip uninstall thinc cymem && pip install spacy` for spaCy-related issues) to ensure compatible versions are installed.","cause":"This typically indicates a version mismatch between `cymem` and another library that depends on it (e.g., `spaCy` or `thinc`), where the dependent library expects an older or newer API from `cymem` that is not present.","error":"AttributeError: module 'cymem.cymem' has no attribute 'PyMalloc'"},{"fix":"Upgrade your Cython package to a newer version that supports your Python interpreter version using `pip install --upgrade Cython`. Ensure your compiler (e.g., MSVC on Windows) is correctly set up and compatible with your Python version.","cause":"This is a C compilation error that arises when `cymem` (or its Cython-generated C source) is being compiled with an older version of Cython against a newer Python interpreter (specifically Python 3.9+ which removed `PyTypeObject.tp_print`).","error":"error C2039: 'tp_print': is not a member of '_typeobject'"}],"ecosystem":"pypi","meta_description":null,"install_score":0,"install_tag":"stale","quickstart_score":null,"quickstart_tag":null,"pypi_latest":"2.0.13","cli_name":"","install_checks":{"last_tested":"2026-05-12","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"18.4M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":null,"mem_mb":null,"disk_size":"19M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"20.2M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.6,"import_time_s":null,"mem_mb":null,"disk_size":"21M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"12.2M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.4,"import_time_s":null,"mem_mb":null,"disk_size":"13M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"11.9M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":null,"mem_mb":null,"disk_size":"13M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"17.9M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.8,"import_time_s":null,"mem_mb":null,"disk_size":"19M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null}]},"quickstart_checks":{"last_tested":"2026-04-24","tag":null,"tag_description":null,"results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}}