一.非容器启动

1.第一步

#第一步:通过jps命令确认jvm进程号
[root@dataprocess-server]# jps -l
19570 customer-datap-1.3.2.jar
10589 sun.tools.jps.Jps

2.第二步

#第二步:通过jmap命令dump堆内存文件到指定目录
[root@dataprocess-server]# jmap -dump:format=b,file=/temp/dump.thprof 19570
Dumping heap to /temp/dump.thprof ...
Heap dump file created

3.OOM 自动生成文件

配置OOM自动生成dump文件:

#JVM在发生OutOfMemoryError时生成Heap Dump文件
java -XX:+HeapDumpOnOutOfMemoryError -jar your_application.jar

#指定Heap Dump文件的输出路径
java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/path/to/dump/files -jar your_application.jar

#当发生OutOfMemoryError时,执行指定的命令
java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/path/to/dump/files -XX:OnOutOfMemoryError="kill -9 %p" -jar your_application.jar

二.容器启动

1.容器的基本信息

#docker容器的基本信息
docker stats
#这里的PIDS是容器内的PID,基本上没什么用

【JVM调优系列】如何导出堆内存文件-LMLPHP

2.Dockerfile 配置

FROM openjdk:latest

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone

WORKDIR /home
USER root

ENV PROFILE="dev"
ENV LC_ALL en_US.UTF-8
ENV LANG en_US.UTF-8
ENV JAVA_OPTS="\
-Xms4g \
-Xmx4g \
-Xmn2g \
-Xss1m \
-XX:SurvivorRatio=8 \
-XX:MaxTenuringThreshold=10 \
-XX:+UseConcMarkSweepGC \
-XX:CMSInitiatingOccupancyFraction=70 \
-XX:+UseCMSInitiatingOccupancyOnly \
-XX:+AlwaysPreTouch \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:HeapDumpPath=:./logs/gc \
-verbose:gc \
-XX:+PrintGCDetails \
-XX:+PrintGCDateStamps \
-XX:+PrintGCTimeStamps \
-Xloggc:./logs/gc/gc.log \
"

ENV PARAMS=""

COPY ./insight-provider/target/*.jar /home/app.jar

EXPOSE 80

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
RUN echo -e 'mkdir -p ./logs/gc && java $JAVA_OPTS -jar ./app.jar --spring.profiles.active=$PROFILE $PARAMS' > entrypoint.sh

ENTRYPOINT ["sh", "entrypoint.sh"]

3.启动脚本

#!/usr/bin/env bash
CONTAINER_NAME=insight-command
IMAGE_NAME=xxx.xxx.com/xxx-uat/xxx-xx-xx-insight-command:$1
docker rm -f ${CONTAINER_NAME}
docker rmi ${IMAGE_NAME}
docker pull ${IMAGE_NAME}
docker run -d --name ${CONTAINER_NAME} \
--privileged=true \
-e PROFILE=uat \
-e PARAMS="--logging.level.root=info" \
-w /home \
-p 8090:80 \
-v $PWD/logs:/home/logs \
-v /home/uploads:/home/uploads \
--restart=always ${IMAGE_NAME}
docker logs -f  --tail 500  ${CONTAINER_NAME}
01-17 10:15