Do java flags Xms and Xmx overwrite flag XX:+UseCGroupMemoryLimitForHeap?

后端 未结 2 1030
猫巷女王i
猫巷女王i 2020-12-19 02:55

I\'m running a containerized java application in Kubernetes.

In order to make the jvm reserve memory according to the container specifications, the flags -XX:+

相关标签:
2条回答
  • 2020-12-19 03:41

    Think about it this way, before that UseCGroupMemoryLimitForHeap was added, you could specify Xmx with a value larger than what your pod had (it would look only on the host memory, not the pod itself), eventually being killed. By default that was 1/4 of the memory if not specified. This happens because heap is calculated like:

    heap = memory / MaxRAMFraction
    

    And running:

    java -XX:+PrintFlagsFinal | grep MaxRAMFraction
    

    would reveal that MaxRAMFraction = 4.

    Now that UseCGroupMemoryLimitForHeap was added, you can tell how much heap from the pod itself will be taken. By default, that is still 1/4; but you can adjust that via MaxRAMFraction, of course.

    If you specify both arguments, Xms wins. always. And 1) this is pretty logic (as UseCGroupMemoryLimitForHeap is more specific than Xmx) 2) this is exactly proven by my experiments just now. No matter the order in which they are specified, Xmx always wins.

    0 讨论(0)
  • 2020-12-19 03:55

    The -Xmx flag overwrites the -XX:+UseCGroupMemoryLimitForHeap flag.

    The flag -XX:+UseCGroupMemoryLimitForHeap lets the JVM detect what the max heap size in a container should be.

    The -Xmx flag set the max heap size to a fixed size.

    To answer your example, the JVM would reserve 2500M heap space. There will be some additional memory usage for non-heap and jvm stuff.

    To further tune your memory usage in a container you could use the -XX:MaxRAMFraction flag. See this article: https://blog.csanchez.org/2017/05/31/running-a-jvm-in-a-container-without-getting-killed/

    0 讨论(0)
提交回复
热议问题