E_WARNING php system_error ai_generated partial

警告:Zend OPcache huge_code_pages:madvise(HUGEPAGE)失败:无法分配内存,位置未知,第0行

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

ID: php/opcache-memory-corruption

其他格式: JSON · Markdown 中文 · English
90%修复率
85%置信度
1证据数
2024-03-15首次发现

版本兼容性

版本状态引入弃用备注
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

根因分析

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

English

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

官方文档

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

解决方案

  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.

无效尝试

常见但无效的做法:

  1. 95% 失败

    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% 失败

    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.