运行eclipse-temurin:17-jdk Docker镜像时出现内存不足问题
解决Docker容器内Java GC线程创建失败问题
问题场景
在运行eclipse-temurin:17-jdk镜像的Docker容器内执行./mvnw clean package时,Maven尚未启动就报错,错误信息如下:
[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
hs_err_pid8.log的详细内容:
There is insufficient memory for the Java Runtime Environment to continue.Cannot create worker GC thread. Out of system resources.Possible reasons:The system is out of physical RAM or swap spaceThe process is running with CompressedOops enabled, and the Java Heap may be blocking the growth of the native heapPossible solutions:Reduce memory load on the systemIncrease physical memory or swap spaceCheck if swap backing store is fullDecrease Java heap size (-Xmx/-Xms)Decrease number of Java threadsDecrease Java thread stack sizes (-Xss)Set larger code cache with -XX:ReservedCodeCacheSize=JVM is running with Zero Based Compressed Oops mode in which the Java heap isplaced in the first 32GB address space. The Java Heap base address is themaximum limit for the native heap growth. Please use -XX:HeapBaseMinAddressto set the Java Heap base and to place the Java Heap above 32GB virtual address.This output file may be truncated or incomplete.Out of Memory Error (workerManager.hpp:87), pid=8, tid=8JRE 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)
解决方案
1. 调整Docker容器内存配额
如果容器分配的内存不足,直接增加内存限制:
- 运行容器时添加参数:
docker run -m 2g ...(2g表示分配2GB内存,可按需调整) - 使用Docker Compose时,在服务配置中添加:
services: your-service: mem_limit: 2g
2. 降低JVM内存参数
通过环境变量或修改mvnw脚本,减小JVM堆内存和线程栈大小:
- 临时执行命令:
export MAVEN_OPTS="-Xmx512m -Xss256k" && ./mvnw clean package - 永久修改:编辑
./mvnw脚本,在JVM启动部分加入上述参数。
3. 检查并扩容宿主机Swap空间
- 查看Swap使用情况:
free -h - 临时增加Swap文件:
sudo fallocate -l 2G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile - 永久生效:将
/swapfile none swap sw 0 0添加到/etc/fstab文件。
4. 调整CompressedOops配置(大内存场景)
若宿主机内存超过32GB,可尝试以下操作:
- 关闭CompressedOops:
export MAVEN_OPTS="-XX:-UseCompressedOops" && ./mvnw clean package - 调整堆基地址,将Java堆放到32GB以上地址空间:
export MAVEN_OPTS="-XX:HeapBaseMinAddress=0x1000000000" && ./mvnw clean package
5. 减少系统资源负载
- 关闭宿主机或容器内无关进程,释放内存和线程资源;
- CI/CD环境中,控制同时运行的任务数量,避免资源耗尽。
内容的提问来源于stack exchange,提问作者Anthony O.
相关产品推荐
相关产品推荐

