note.wcoder.com
wcoder GitHub

Table of Contents

默认情况下,JVM的Max Heap Size是系统内存的1/4; 我们可以使用jmap -head pid 查看启动时的Max Heap Size

JDK版本 JVM参数
<8u131 -Xms64m -Xmx128m
8u131-191 -XX:+UnlockExperimentalVMOptions、-XX:+UseCGroupMemoryLimitForHeap

8u191 |-XX:UseContainerSupport(默认启用)、ActiveProcessorCount;百分比分配堆内存:MaxRAMPercentage、InitialRAMPercentage、MinRAMPercentage

JDK8版本小于131时,启动JAVA程序时,添加参数 -Xms64m -Xmx128m;java 8u131+和java 9+版本,添加两个参数:-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap;JDK8版本高于191时,可以使用 MaxRAMPercentage,MaxRAMPercentage 值介于 0.0 到 100.0 之间,默认值为 25.0。

dockerfile中可以这样写
ENV _JAVA_OPTIONS="-XX:+UseContainerSupport -XX:InitialRAMPercentage=50 -XX:MaxRAMPercentage=80" 使用JAVA_OPTS好像没有效果,就使用_JAVA_OPTIONS
docker 运行限制内存docker run -m 1G

k8s中这样设置

containers:
    resources:
      requests:
        memory: "512Mi"
        # cpu: "500m"
      limits:
        memory: "512Mi"
        # cpu: "500m"
    env:
      - name: _JAVA_OPTIONS
        values: -XX:MaxRAMPercentage=75.0

jdk11使用 jhsdb jmap --heap --pid 1 查看堆大小

jdk1.8使用jmap -heap 1 查看堆大小

可以使用jinfo -flag MaxRAMPercentage 1查看MaxRAMPercentage参数有没有生效,1 是pid

dockerfile & yaml

dockerfile

FROM openjdk:17-slim-bullseye as base
WORKDIR /app
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
ENV _JAVA_OPTIONS="-XX:InitialRAMPercentage=75 -XX:MaxRAMPercentage=75"

FROM maven:3.8-openjdk-17-slim as build
WORKDIR /src
# 这里可以修改settings.xml
# 方案一:修改镜像中的settings.xml ;
# 方案二: 把settings.xml复制到源代码中,copy到镜像中后 使用mvn命令指定settings.xml
# 方案三:使用BuildKit缓存, RUN --mount=type=cache,target=/root/.m2 mvn package --settings settings.xml -Dmaven.test.skip=true -U -e -X -B
# COPY /root/.m2 /root/.m2
# -P dev
RUN mvn package --settings settings.xml -Dmaven.test.skip=true -U -e -X -B



# 也可以只拷贝需要的文件和目录
# 工作目录在镜像的 /build 目录下
# WORKDIR /build/
# 把本地的 pom.xml 和 src目录 复制到镜像的/build目录下
# COPY pom.xml /build/
# COPY src /build/src/


FROM base as final
WORKDIR /app
COPY --from=build /src/target/springboot-0.0.1-SNAPSHOT.jar .
ENTRYPOINT ["java", "-jar", "springboot-0.0.1-SNAPSHOT.jar"]



#FROM openjdk:17-slim-bullseye
#WORKDIR /app
#RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#RUN echo 'Asia/Shanghai' >/etc/timezone
#ENV JAVA_OPTS="-XX:InitialRAMPercentage=75 -XX:MaxRAMPercentage=75"
#
#WORKDIR /app
#EXPOSE 80
#ADD ./target/springboot-0.0.1-SNAPSHOT.jar .

# docker build -t springbootdemo:v1 .
# docker run -it --rm -p 9808:80 springbootdemo:v1
# 访问http://localhost:9808/test/get

yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: springbootdemo-deployment
  labels:
    app: springbootdemo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: springbootdemo
  template:
    metadata:
      labels:
        app: springbootdemo
    spec:
      containers:
        - name: springbootdemo
          image: springbootdemo:v1
          ports:
            - containerPort: 80
          env:
            - name: _JAVA_OPTIONS
              value: "-XX:MaxRAMPercentage=75.0"
          resources:
            limits:
              cpu: 1000m
              memory: 512Mi
            requests:
              cpu: 1000m
              memory: 512Mi

---

apiVersion: v1
kind: Service
metadata:
  name: springbootdemo-service
spec:
  selector:
    app: springbootdemo
  type: NodePort
  ports:
    - protocol: TCP
      port: 80 # 服务访问端口
      targetPort: 80 # 容器端口
      nodePort: 30001  # NodePort curl 127.0.0.1:30001/test/get

参考

Java/Spring应用在k8s环境中的内存配置实践

jdk1.8.191 JVM内存参数 InitialRAMPercentage和MinRAMPercentage

Kubernetes_pod_javajdk_动态JVM堆内存大小限制

← Previous Next →
Less
More