That’s quite confusing but Xmx=1g lets my JVM consuming more than 1g of RAM. How so?
Doing some tests here I see that when using Xmx=1g, the JVM is actually using 1.5g! well the explanation is simple:
Xmx only limits the allocated memory on the heap.
So how to limit it??
To specify the total usage you need to use –XX:MaxRAM.
Containers update: Although not clearly stated, when I originally wrote this solution MaxRAM is being used on containers because at that time ( before java 8u191 or so) the java wouldn’t recognize the size of the container. So user would need to go there and set the size of the MaxRAM. So I mean to have a limit of total size of the container (heap and off-heap) and MaxRAM does exactly that. However, this flag is less useful currently given the JVM is container aware – so the JVM will know the limits of the memory via cgroups and then the heap and off-heap is usually be set as starting flags. So then MaxRAM is almost retired if we think about. So I mean it is not a flag we will always use – but of course depending on context we can still use it. For instance if you want to have a container (with or without a sidecar) with a certain size of memory and leave a little bit of space with the max memory Java container can use (like pod == 4gb and container uses 3.95gb – leaving 50mb for other troubleshooting tools for example – that can be useful to set MaxRAM maybe similar scenarios. So basically 8u191+ == container aware – so no need for MaxRAM.
| 8u191+ –> container aware |