<p>I am running several Java Applications with the Docker image <a href="https://hub.docker.com/r/jboss/wildfly" rel="nofollow noreferrer">jboss/wildfly:20.0.1.Final</a> on Kubernetes 1.19.3. Wildfly server is running in OpenJDK 11 so the jvm is supporting container memory limits (cgroups).</p>
<p>If I set a memory limit this limit is totally ignored by the container when running in Kubernetes. But it is respected on the same machine when I run it in plain Docker:</p>
<p><strong>1. Run Wildfly in Docker with a memory limit of 300M:</strong></p>
<pre><code>$ docker run -it --rm --name java-wildfly-test -p 8080:8080 -e JAVA_OPTS='-XX:MaxRAMPercentage=75.0' -m=300M jboss/wildfly:20.0.1.Final
</code></pre>
<p>verify Memory usage:</p>
<pre><code>$ docker stats
CONTAINER ID        NAME                 CPU %        MEM USAGE / LIMIT     MEM %       NET I/O       BLOCK I/O     PIDS
515e549bc01f        java-wildfly-test    0.14%        219MiB / 300MiB       73.00%      906B / 0B     0B / 0B       43
</code></pre>
<p>As expected the container will NOT exceed the memory limit of 300M.</p>
<p><strong>2. Run Wildfly in Kubernetes with a memory limit of 300M:</strong></p>
<p>Now I start the same container within kubernetes.</p>
<pre><code>$ kubectl run java-wildfly-test --image=jboss/wildfly:20.0.1.Final --limits='memory=300M' --env=&quot;JAVA_OPTS='-XX:MaxRAMPercentage=75.0'&quot; 
</code></pre>
<p>verify memory usage:</p>
<pre><code>$ kubectl top pod java-wildfly-test
NAME                CPU(cores)   MEMORY(bytes)   
java-wildfly-test   1089m        441Mi 
</code></pre>
<p>The memory limit of 300M is totally ignored and exceeded immediately.</p>
<p>Why does this happen? Both tests can be performed on the same machine.</p>
<p><strong>Answer</strong></p>
<p>The reason for the high values was an incorrect output of the Metric data received from Project <a href="https://github.com/prometheus-operator/kube-prometheus" rel="nofollow noreferrer">kube-prometheus</a>. After uninstalling the kube-projemet and installing instead the <a href="https://github.com/kubernetes-sigs/metrics-server" rel="nofollow noreferrer">metric-server</a> all data was display correctly using kubctl top. It shows now the same values as docker stats. I do not know why kube-prometheus did compute wrong data. In fact it was providing the double values for all memory data.</p>

<p>I`m placing this answer as community wiki since it might be helpful for the community. Kubectl top was displying incorrect data. OP solved the problem with uninstalling the kube-prometheus stack and installing the metric-server.</p>
<blockquote>
<p>The reason for the high values was an incorrect output of the Metric
data received from Project kube-prometheus. After uninstalling the
kube-projemet and installing instead the metric-server all data was
display correctly using kubectl top. It shows now the same values as
docker stats. I do not know why kube-prometheus did compute wrong
data. In fact it was providing the double values for all memory data.</p>
</blockquote>
