E_WARNING php system_error ai_generated partial

Warning: Zend OPcache huge_code_pages: madvise(HUGEPAGE) failed: Cannot allocate memory in Unknown on line 0

ID: php/opcache-memory-corruption

Also available as: JSON · Markdown · 中文
90%Fix Rate
85%Confidence
1Evidence
2024-03-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
PHP 8.1 active
PHP 8.2 active
PHP 8.3 active
PHP 8.4 active
Linux kernel 5.15+ active
Linux kernel 6.x active

Root Cause

OPcache's huge code pages feature fails when the system's transparent hugepage support is misconfigured or the kernel cannot allocate contiguous huge pages, often due to memory fragmentation or insufficient hugepage pool size.

generic

中文

OPcache的大页代码功能在系统透明大页支持配置错误或内核无法分配连续大页时失败,通常由内存碎片化或大页池大小不足引起。

Official Documentation

https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.huge-code-pages

Workarounds

  1. 95% success Disable huge code pages in php.ini by setting `opcache.huge_code_pages=0`, then restart PHP-FPM: `sudo systemctl restart php8.2-fpm`
    Disable huge code pages in php.ini by setting `opcache.huge_code_pages=0`, then restart PHP-FPM: `sudo systemctl restart php8.2-fpm`
  2. 85% success Configure transparent hugepages on the system: run `echo always > /sys/kernel/mm/transparent_hugepage/enabled` and ensure `vm.nr_hugepages` is set high enough in `/etc/sysctl.conf` (e.g., `vm.nr_hugepages=1024`), then reboot.
    Configure transparent hugepages on the system: run `echo always > /sys/kernel/mm/transparent_hugepage/enabled` and ensure `vm.nr_hugepages` is set high enough in `/etc/sysctl.conf` (e.g., `vm.nr_hugepages=1024`), then reboot.

中文步骤

  1. Disable huge code pages in php.ini by setting `opcache.huge_code_pages=0`, then restart PHP-FPM: `sudo systemctl restart php8.2-fpm`
  2. Configure transparent hugepages on the system: run `echo always > /sys/kernel/mm/transparent_hugepage/enabled` and ensure `vm.nr_hugepages` is set high enough in `/etc/sysctl.conf` (e.g., `vm.nr_hugepages=1024`), then reboot.

Dead Ends

Common approaches that don't work:

  1. 95% fail

    The error stems from the OS kernel state, not the PHP process; a restart only clears temporary state but does not fix the underlying hugepage allocation failure.

  2. 90% fail

    This directive controls the size of the shared memory segment for cached scripts, not the huge page mechanism. The error is triggered during huge page allocation, independent of memory consumption.