容器内执行脚本时报错如下,Java 进程异常退出

library initialization failed - unable to allocate file descriptor table - out of memory./single.sh: line 314:  1205 Aborted                 (core dumped) $JAVACMD $JAVA_OPTS -cp $CLASS_PATH -jar $DEFAULT_JAR $APP_NAME

一、根本原因


该问题源于 Java 8 的 JDK Bug(JDK-8150460):

Java 8 在启动时,会尝试根据当前系统 ulimit -n 值,预分配文件描述符表的内存块;
在现代系统中默认 ulimit -n 值通常为 65535 或更高;
在容器资源受限或配置不当的情况下,native 层内存不足,JVM 启动失败,抛出 unable to allocate file descriptor table。
此外还有两个系统环境相关的影响:

旧版本的 systemd 不支持 LimitNOFILE=infinity 生效;
Docker 容器的默认 ulimit 配置可能不足,或者过大触发此问题。

解决方法


1、限制 Docker 容器的文件描述符上限将容器启动参数中显式设置 ulimit -n:通过在容器中执行ulimit -n 查询容器是多少

--ulimit nofile=1024:1024

docker run --ulimit nofile=1024:1024 -d -p 8080:8080 your-image

或者在 docker-compose.yml 中配置:

ulimits:
  nofile:
    soft: 1024
    hard: 1024

推荐设置为 8192 以下,如 1024、4096 等。

引用:

1、【Docker 运维】Java 应用在 Docker 容器中启动报错:`unable to allocate file descriptor table`_library initialization failed - unable to allocate-CSDN博客

2、docker java library initialization failed - unable to allocate file descriptor table - out of memory-CSDN博客

更多推荐