{"record":{"id":"b78518813d2516d4","repo":"pola-rs/polars","slug":"could-not-allocate-memory-for-cpuid-check","errorCode":null,"errorMessage":"could not allocate memory for CPUID check","messagePattern":"could not allocate memory for CPUID check","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_cpu_check.py","lineNumber":176,"sourceCode":"            opc = _POSIX_64_OPC if _IS_64BIT else _CDECL_32_OPC\n\n        size = len(opc)\n        code = (ctypes.c_ubyte * size)(*opc)\n\n        if _IS_WINDOWS:\n            self.win.VirtualAlloc.restype = c_void_p\n            self.win.VirtualAlloc.argtypes = [\n                ctypes.c_void_p,\n                ctypes.c_size_t,\n                ctypes.c_ulong,\n                ctypes.c_ulong,\n            ]\n            self.addr = self.win.VirtualAlloc(\n                None, size, _MEM_COMMIT | _MEM_RESERVE, _PAGE_EXECUTE_READWRITE\n            )\n            if not self.addr:\n                msg = \"could not allocate memory for CPUID check\"\n                raise MemoryError(msg)\n            ctypes.memmove(self.addr, code, size)\n        else:\n            import mmap  # Only import if necessary.\n\n            # On some platforms PROT_WRITE + PROT_EXEC is forbidden, so we first\n            # only write and then mprotect into PROT_EXEC.\n            libc = _open_posix_libc()\n            mprotect = libc.mprotect\n            mprotect.argtypes = (ctypes.c_void_p, ctypes.c_size_t, ctypes.c_int)\n            mprotect.restype = ctypes.c_int\n\n            self.mmap = mmap.mmap(\n                -1,\n                size,\n                mmap.MAP_PRIVATE | mmap.MAP_ANONYMOUS,\n                mmap.PROT_READ | mmap.PROT_WRITE,\n            )\n            self.addr = ctypes.addressof(ctypes.c_void_p.from_buffer(self.mmap))","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/_cpu_check.py#L158-L194","documentation":"At import time on Windows, polars allocates a small executable page via VirtualAlloc (MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE) to hold a CPUID stub that detects CPU features (sse4.2, avx2, ...) before choosing/validating the Rust runtime variant. This MemoryError means VirtualAlloc returned NULL: the OS refused to reserve/commit the page, almost always because commit charge (RAM + pagefile) is exhausted or a per-process/job commit limit was reached.","triggerScenarios":"`import polars` on Windows while the system is out of committable memory: pagefile disabled or capped, a memory-hungry parent process, or a job-object commit limit (CI runners, containers on Windows). The allocation happens in CPUID.__init__ during check_cpu_flags, before the Rust runtime is selected.","commonSituations":"Windows CI agents with small commit limits; machines with pagefile disabled for 'performance'; long-running processes that leaked memory before first importing polars; batch jobs launched after several heavy processes filled the commit limit.","solutions":["Free memory or restart the process before importing polars","Increase the Windows pagefile (or set it to system-managed) so commit charge is available","Raise the commit limit of the job object/container the process runs under","As a workaround, set `POLARS_SKIP_CPU_CHECK=1` before importing polars — the CPUID probe (and thus the allocation) is skipped entirely, at the risk of running an incompatible runtime"],"exampleFix":"# before\nimport polars as pl  # MemoryError: could not allocate memory for CPUID check\n\n# after\nimport os\nos.environ[\"POLARS_SKIP_CPU_CHECK\"] = \"1\"  # skip the CPUID allocation\nimport polars as pl","handlingStrategy":"fallback","validationCode":"import ctypes\n\nclass MEMORYSTATUSEX(ctypes.Structure):\n    _fields_ = [(\"dwLength\", ctypes.c_ulong), (\"dwMemoryLoad\", ctypes.c_ulong),\n                (\"ullTotalPhys\", ctypes.c_uint64), (\"ullAvailPhys\", ctypes.c_uint64),\n                (\"ullTotalPageFile\", ctypes.c_uint64), (\"ullAvailPageFile\", ctypes.c_uint64),\n                (\"ullTotalVirtual\", ctypes.c_uint64), (\"ullAvailVirtual\", ctypes.c_uint64),\n                (\"ullAvailExtendedVirtual\", ctypes.c_uint64)]\n\nst = MEMORYSTATUSEX(); st.dwLength = ctypes.sizeof(MEMORYSTATUSEX)\nctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(st))\nif st.ullAvailPageFile < 64 * 1024 * 1024:  # < 64 MB commit available\n    import os; os.environ[\"POLARS_SKIP_CPU_CHECK\"] = \"1\"","typeGuard":null,"tryCatchPattern":"import sys\ntry:\n    import polars as pl\nexcept MemoryError:\n    # CPUID probe could not allocate its page; retry once without the probe\n    import os\n    os.environ[\"POLARS_SKIP_CPU_CHECK\"] = \"1\"\n    for m in [m for m in sys.modules if m.startswith(\"polars\")]:\n        del sys.modules[m]\n    import polars as pl  # noqa","preventionTips":["Keep the Windows pagefile system-managed (or sized generously) on machines that run polars","Import polars early in the process, before large allocations consume commit charge","In memory-constrained CI, set POLARS_SKIP_CPU_CHECK=1 up front when CPU features are known-good","Watch commit charge (not just working set) when sizing containers on Windows"],"tags":["windows","memory","import-time","cpu-flags","virtualalloc"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}