<p>I would like to know,  if there is an optimal approach for setting memory limits for Kubernetes containers, especially for applications running java.</p>
<p>For Java applications, we have to set a heap value in conjunction with Kubernetes resources. it's like we're kinda forced to guess at a memory limit for the Kubernetes microservice.</p>
<p>To be more clear,</p>
<ul>
<li>in java the heap memory can be limited to memory limits defined in the container, but how to arrive at a specific limit value ??</li>
<li>If we don't set up limits for the container, then the java heap considers the underlying node memory limits rather than at container limits, so it can extend the pod limit to max memory,  which can stress the other pods running in that node.</li>
<li>If we don't set up enough memory limits at the container, then we can see containers getting killed with OOM errors.</li>
</ul>
<p>The possible solutions, I can think of is</p>
<ol>
<li>Monitoring the microservice for some period of time and based on the utilization, choosing the limits</li>
<li>Implementing some load testing mechanism and based on the observation setting the limits</li>
</ol>
<p>Other than the above, I would like to get some comments if there is any other approach followed by anyone in setting memory limits for Kubernetes containers.
Has anyone encountered this earlier!!</p>

<p>Yes, I have encountered the issue multiple times. You definitely want to keep the memory limit for the k8 to avoid the noisy neighbour problems. The possible solutions you have mentioned are right. Monitoring and load testing are a must to arrive at the number.</p>
<p>Along with these, I used the profiling of Java processes to see how GC is getting triggered and whether the memory usage should remain the same or increase with the increase of load. Profiling is a very powerful tool to provide some insights into suboptimal usage of data structures as well.</p>
<p><strong>What to profile</strong></p>
<p>While doing the Java profiling, you need to check</p>
<ul>
<li>What's the Eden and old-gen usage</li>
<li>How often full GC is running, the memory utilisation will increase and decrease after the full GC. See the <a href="https://dzone.com/articles/interesting-garbage-collection-patterns" rel="nofollow noreferrer">GC pattern</a></li>
<li>How many objects are getting created</li>
<li>CPU usage, (will increase during the full GC)</li>
</ul>
<p><strong>How to profile Java application</strong></p>
<p>Here are a few good resources</p>
<ul>
<li><a href="https://www.baeldung.com/java-profilers#:%7E:text=A%20Java%20Profiler%20is%20a,thread%20executions%2C%20and%20garbage%20collections" rel="nofollow noreferrer">https://www.baeldung.com/java-profilers#:~:text=A%20Java%20Profiler%20is%20a,thread%20executions%2C%20and%20garbage%20collections</a>.</li>
<li><a href="https://medium.com/platform-engineer/guide-to-java-profilers-e344ce0339e0" rel="nofollow noreferrer">https://medium.com/platform-engineer/guide-to-java-profilers-e344ce0339e0</a></li>
</ul>
<p><strong>How to Profile Kubernetes Application with Java</strong></p>
<ul>
<li><a href="https://medium.com/swlh/introducing-kubectl-flame-effortless-profiling-on-kubernetes-4b80fc181852" rel="nofollow noreferrer">https://medium.com/swlh/introducing-kubectl-flame-effortless-profiling-on-kubernetes-4b80fc181852</a></li>
<li><a href="https://www.youtube.com/watch?v=vHTWdkCUAoI" rel="nofollow noreferrer">https://www.youtube.com/watch?v=vHTWdkCUAoI</a></li>
</ul>
