<p>Am deploying springboot application in kubernets using Jib. When the service starting the memory usage is around 300MB but it grows up to 1.3gb over time. How to avoid this increase without any usage? The application is up and running. The API gateways are not open to user now still the memory is incrementing over time.</p>
<p>kubernets deployment configuration</p>
<pre><code># Source: services/charts/login/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/instance: release-name
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: login
    app.kubernetes.io/version: 1.16.0
  name: login
spec:
  selector:
    matchLabels:
      app.kubernetes.io/instance: release-name
      app.kubernetes.io/name: login
  template:
    metadata:
      labels:
        app.kubernetes.io/instance: release-name
        app.kubernetes.io/name: login
    spec:
      containers:
      - env:
        - name: APP_NAME
          value: login-release-name
        - name: JAVA_TOOL_OPTIONS
          value: -Dspring.profiles.active=prod
        image: dockerregistry.com/login:1.0.0
        imagePullPolicy: Always
        lifecycle:
          preStop:
            exec:
              command:
              - sh
              - -c
              - sleep 10
        livenessProbe:
          httpGet:
            path: /actuator/health
            port: 8080
          initialDelaySeconds: 30
        name: login
        ports:
        - containerPort: 8080
          name: http
          protocol: TCP
        readinessProbe:
          httpGet:
            path: /actuator/health
            port: 8080
          initialDelaySeconds: 30
        resources:
          limits:
            cpu: 2000m
            memory: 1Gi
          requests:
            cpu: 100m
            memory: 1Gi
      imagePullSecrets:
      - name: regcred
      terminationGracePeriodSeconds: 60
</code></pre>
<p>spring boot configuration for kubernets</p>
<pre><code>server.port=8080
server.shutdown=graceful
spring.lifecycle.timeout-per-shutdown-phase=45s

server.tomcat.accept-count=100
server.tomcat.max-connections=8000
server.tomcat.connection-timeout=10000
server.tomcat.max-threads=200
server.tomcat.min-spare-threads=10

spring.datasource.url=jdbc:postgresql://${DB_HOST:#{&quot;postgres&quot;}}/postgres
spring.datasource.username=${DB_USER:#{&quot;postgres&quot;}}
spring.datasource.password=${DB_PASSWORD:#{&quot;na&quot;}}
spring.datasource.type=org.springframework.jdbc.datasource.DriverManagerDataSource
spring.datasource.driver-class-name=org.postgresql.Driver
</code></pre>
<p>Do we need to configure anything to limit the memory usage to 1GB limit? Now the kubernets will kill the pod if it goes beyond 1GB.</p>
<p>am creating the image using the Jib.</p>
<pre><code>mvn compile com.google.cloud.tools:jib-maven-plugin:3.3.0:dockerBuild -Dimage=login -DskipTests
</code></pre>

PRAJIN PRAKASH
<p><strong>Update 05 02 2023</strong>
Changing the JVM Runtime to OpenJ9 reduced memory footprint dramatically,</p>
<p>also Changed to jre image instead of jdk</p>
<p>Updated command
<code>mvn compile com.google.cloud.tools:jib-maven-plugin:3.3.0:dockerBuild -Djib.from.image=ibm-semeru-runtimes:open-8-jre -Dimage=$imageName -DskipTests</code></p>
<p><strong>OLD Answer</strong></p>
<p>After a long analysis I found that there is no memory leak from the application.
It was due to the container so I switched to <code>openjdk:8-jdk-alpine</code> using <code>Djib.from.image=</code> option in Jib</p>
<p>first I added some JVM option to set The JVM Heap But its also behaving same.</p>
<p><code>-XX:+UseG1GC -Xms100M -Xmx1G</code> (Garbage collection changed and set max heap to 1GB</p>
<p>using <a href="https://visualvm.github.io/download.html" rel="nofollow noreferrer">VisualVM</a> suggest by @Mihai Pasca I analyzed the memory usage in the container.</p>
<p>updated command of Jib</p>
<pre><code>mvn compile com.google.cloud.tools:jib-maven-plugin:3.3.0:dockerBuild -Djib.from.image=openjdk:8-jdk-alpine -Dimage=$imageName -DskipTests  -Djib.container.environment=JAVA_TOOL_OPTIONS=&quot;-XX:+UseG1GC -Xms100M -Xmx1G -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=9010 -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.rmi.port=9010 -Djava.rmi.server.hostname=localhost&quot;  -Djib.container.ports=8080,9010 
 
</code></pre>
<p>And connected to 9010 from visual vm using add JMX option.</p>
<p><a href="https://i.stack.imgur.com/q2cdE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q2cdE.png" alt="java memory usage in visual vm" /></a></p>
<p>There is no over usage I found and the GC works fine</p>
<p>And memory usage by the container also fine and not growing continuesly its gorwing up and come down if no use. Previusly its continuly growing.</p>
<p><a href="https://i.stack.imgur.com/6MNSQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6MNSQ.png" alt="docker desktop memory usage" /></a></p>
<p>now I started the same in k8s working fine now.</p>
