<p>I have an openjdk:8 image running on the Kubernetes cluster. I added memory HPA (Horizontal Pod Autoscaling) which scales up fine but since JVM doesn't release the memory back from the heap to the OS, pods do not scale down. Following is the hpa.yaml</p>
<pre><code>apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: image-server
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: image-server
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 60
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 60
</code></pre>
<p>One way to solve this is to use the right GC and make it release the memory, but since JVM has been designed to not release from the heap frequently for performance reasons, doing this isn't a good idea. Is there a way to handle this from Kubernetes? Like instead of checking OS's memory usage, can we not just check the memory usage from heap and scale on that?</p>

<p>Scaling Java applications in Kubernetes is a bit tricky. The HPA looks at system memory only and as pointed out, the JVM generally do not release commited heap space (at least not immediately).</p>
<p>There are two main approaches one could take to solve this</p>
<h2>1. Tune JVM Parameters so that the commited heap follows the used heap more closely</h2>
<p>Depending on which JVM and GC is in use the tuning options may be slightly different, but the most important ones would be</p>
<ul>
<li><code>MaxHeapFreeRatio</code> - How much of the commited heap that is allowed to be unused</li>
<li><code>GCTimeRatio</code> - How often GC is allowed to run (impacts performance)</li>
<li><code>AdaptiveSizePolicyWeight</code> - How to weigh older vs newer GC runs when calculating new heap</li>
</ul>
<p>Giving exact values for these are not easy, it is a compromise between releasing memory fast and application performance. The best settings will be dependant on the load characteristics of the application.</p>
<p>Patrick Dillon has written an article published by RedHat called <a href="https://cloud.redhat.com/blog/scaling-java-containers" rel="noreferrer">Scaling Java containers</a> that deep dives into this subject.</p>
<h2>2. Custom scaling logic</h2>
<p>Instead of using the HPA you could create your own scaling logic and deploy it into Kubernetes as a job running periodically to do:</p>
<ol>
<li>Check the heap usage in all pods (for example by running jstat inside the pod)</li>
<li>Scale out new pods if the max threshold is reached</li>
<li>Scale in pods if the min threshold is reached</li>
</ol>
<p>This approach has the benefit of looking at the actual heap usage, but requires a custom component.</p>
<p>An example of this can be found in the article <a href="https://www.powerupcloud.com/autoscaling-based-on-cpu-memory-in-kubernetes-part-ii/#1cac" rel="noreferrer">Autoscaling based on CPU/Memory in Kubernetes — Part II</a> by powercloudup</p>
