
当在docker容器中运行java应用程序(例如使用eclipse-temurin:17-jdk镜像执行maven构建)时,可能会遇到一个看似与内存相关的错误,导致java运行时环境无法继续执行:
[0.002s][warning][os,thread] Failed to start thread "GC Thread#0" - pthread_create failed (EPERM) for attributes: stacksize: 1024k, guardsize: 4k, detached. # # There is insufficient memory for the Java Runtime Environment to continue. # Cannot create worker GC thread. Out of system resources. # An error report file with more information is saved as: # /home/myuser/hs_err_pid8.log
这个错误信息,特别是“There is insufficient memory for the Java Runtime Environment to continue”和“Cannot create worker GC thread. Out of system resources.”,很容易让人误以为是容器分配的内存不足。然而,关键的线索在于pthread_create failed (EPERM)。EPERM表示操作不允许,这通常意味着权限问题,而非简单的内存耗尽。
进一步查看生成的hs_err_pidX.log文件,会发现更多详细信息,例如:
# Possible reasons: # The system is out of physical RAM or swap space # The process is running with CompressedOops enabled, and the Java Heap may be blocking the growth of the native heap # ... # This output file may be truncated or incomplete. # # Out of Memory Error (workerManager.hpp:87), pid=8, tid=8 # # JRE version: (17.0.5+8) (build ) # Java VM: OpenJDK 64-Bit Server VM (17.0.5+8, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, linux-amd64)
尽管日志中列举了多种内存不足的可能原因,并建议减少Java堆大小(-Xmx/-Xms)、减少Java线程数或线程栈大小(-Xss)等常规JVM调优方法,但如果根本原因是EPERM,那么这些JVM参数的调整可能无法解决问题。
Docker利用Linux内核的seccomp(Secure Computing mode)机制来增强容器的安全性。seccomp允许进程限制自身可以执行的系统调用(syscalls)集合。默认情况下,Docker会为容器应用一个预定义的seccomp配置文件,该配置文件禁止了许多不常用或存在潜在安全风险的系统调用。
立即学习“Java免费学习笔记(深入)”;
当Java运行时环境(JRE)尝试创建垃圾回收(GC)线程时,它会调用底层的系统函数(如pthread_create),而这些系统调用可能被Docker默认的seccomp配置文件所限制。当pthread_create操作因seccomp策略被拒绝时,就会返回EPERM错误,导致JRE无法创建必要的GC线程,进而抛出“Cannot create worker GC thread. Out of system resources.”的错误。此时,问题并非实际的内存不足,而是容器的权限限制阻止了Java进程执行其正常操作。
解决此问题的最直接方法是在运行Docker容器时,通过--security-opt seccomp=unconfined参数来禁用Docker的默认seccomp配置文件。unconfined模式意味着容器将以其默认的系统调用权限运行,不再受seccomp的限制。
以下是带有此参数的docker run命令示例:
docker run --rm -it --security-opt seccomp=unconfined eclipse-temurin:17-jdk bash -c "./mvnw clean package"
在上述命令中:
通过添加--security-opt seccomp=unconfined,Java运行时环境将能够成功调用pthread_create来创建GC线程,从而避免“Cannot create worker GC thread. Out of system resources.”的错误,并允许应用程序正常启动和运行。
总之,当在Docker容器中遇到Java应用程序因“内存不足”而无法创建GC线程,并伴随pthread_create failed (EPERM)错误时,最有效的解决方案是调整Docker的seccomp安全策略,通过--security-opt seccomp=unconfined参数解除系统调用限制。这能确保Java运行时环境拥有足够的权限来执行其核心功能。
以上就是Docker中Java应用因seccomp限制导致资源不足的解决方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号