pola-rs/polars · error · MemoryError

could not allocate memory for CPUID check

Error message

could not allocate memory for CPUID check

What it means

In `_cpu_check.py`, the CPUID feature-check class allocates executable memory to run a small CPUID stub (via `VirtualAlloc` on Windows or `mmap`/`mprotect` elsewhere). If the OS refuses the allocation, the constructor raises `MemoryError` with this message. It indicates the process could not obtain the executable memory it needs for the CPU capability probe.

Solutions

  1. Free memory or increase the process/container memory limit, then retry the import.
  2. Check security software (EDR/AV) or sandbox policies blocking PAGE_EXECUTE_READWRITE allocations and allowlist the process.
  3. Run on a supported 64-bit platform/architecture, or use a polars build that skips the runtime CPU check.
Defensive patterns

Strategy: try-catch

Try / catch

try:
    import polars as pl
except MemoryError as e:
    # increase memory limits / disable exec-memory restrictions
    raise RuntimeError('insufficient memory or blocked RWX allocation for polars CPU check') from e

Prevention

When it happens

Trigger: Constructing the CPUID check object on Windows when `VirtualAlloc(None, size, MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE)` returns NULL — e.g. exhausted virtual address space, restrictive security policies, or low-memory conditions.

Common situations: Running polars import inside hardened containers/CI with RWX-memory restrictions; systems under severe memory pressure; 32-bit processes with fragmented address space.

Understand the failure class

Background: "unsupported platform" / "not supported on this platform" errors: what they mean and how to fix them — this error's family across 47 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/b78518813d2516d4. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/_cpu_check.py:176

            opc = _POSIX_64_OPC if _IS_64BIT else _CDECL_32_OPC

        size = len(opc)
        code = (ctypes.c_ubyte * size)(*opc)

        if _IS_WINDOWS:
            self.win.VirtualAlloc.restype = c_void_p
            self.win.VirtualAlloc.argtypes = [
                ctypes.c_void_p,
                ctypes.c_size_t,
                ctypes.c_ulong,
                ctypes.c_ulong,
            ]
            self.addr = self.win.VirtualAlloc(
                None, size, _MEM_COMMIT | _MEM_RESERVE, _PAGE_EXECUTE_READWRITE
            )
            if not self.addr:
                msg = "could not allocate memory for CPUID check"
                raise MemoryError(msg)
            ctypes.memmove(self.addr, code, size)
        else:
            import mmap  # Only import if necessary.

            # On some platforms PROT_WRITE + PROT_EXEC is forbidden, so we first
            # only write and then mprotect into PROT_EXEC.
            libc = _open_posix_libc()
            mprotect = libc.mprotect
            mprotect.argtypes = (ctypes.c_void_p, ctypes.c_size_t, ctypes.c_int)
            mprotect.restype = ctypes.c_int

            self.mmap = mmap.mmap(
                -1,
                size,
                mmap.MAP_PRIVATE | mmap.MAP_ANONYMOUS,
                mmap.PROT_READ | mmap.PROT_WRITE,
            )
            self.addr = ctypes.addressof(ctypes.c_void_p.from_buffer(self.mmap))

View on GitHub (pinned to fe841f959e)