Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Oracle Blog] Java Heap Sizing in a Container: Quickly and Easily

#1
Java Heap Sizing in a Container: Quickly and Easily

In the previous blog, We have seen that Java has made improvements to identify the memory based on a running environment i.e. either a physical machine or a Container (docker). The initial problem with java was that It wasn't able to figure out that it was running in a container and It used to capture the memory for whole hardware where the container was running. (Please see - ttps://blogs.oracle.com/java/java-on-container-like-a-pro)


Now a Java Program running in a container is able to identify the cgroup limit and assign the memory (heap) according to that, (If we do not specify the min and max heap size, which we used to define earlier). So we can run our java program in a container and utilize hardware memory properly, but can we very sure that Java program is using heap size according to cgroup definition?


We have a solution to this problem as XshowSettings:category. This is a handy HotSpot JVM flag (option for the Java launcher java) is the -XshowSettings option. This option is described in the Oracle Java launcher description page as follows:


-XshowSettings:category
Shows settings and continues. Possible category arguments for this option include the following:


all
Shows all categories of settings. This is the default value.


locale
Shows settings related to locale.


properties
Shows settings related to system properties.


vm
Shows the settings of the JVM.


[These types of flags described in the Java tech note - as https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html ]


The showSettings flag provides lots of details about the program running in that JVM, however, our interest is to find out the memory utilization so we will stick to the argument as -> -XshowSettings:vm -version


Let's verify this by running a simple container program without specifying the cgroup flag (which informs java that the program is running in a Java container)


(I set a container memory of 100MB and my JVM sets a max heap of 3.24G)


[root java-8]# docker run -m 100MB oracle-server-jre java -XshowSettings:vm -version


VM settings:


    Max. Heap Size (Estimated): 3.24G


    Ergonomics Machine Class: server


    Using VM: Java HotSpot™ 64-Bit Server VM



java version "1.8.0_181"


Java™ SE Runtime Environment (build 1.8.0_181-b13)


Java HotSpot™ 64-Bit Server VM (build 25.181-b13, mixed mode)



We can clearly see that Java Heap is capturing the heap available to the hardware however the docker size was specified to 100 mb only, Now let's tell Java that the program is running inside the container and see the results.


With java version 1.8.0_181 (including unlock experimental version and Cgroup limit with a specification of memory 1GB MB) with only jdk Docker 


[root]# docker run -m 1GB oracle-server-jre java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XshowSettings:vm -version
VM settings:
    Max. Heap Size (Estimated): 228.00M
    Ergonomics Machine Class: server
    Using VM: Java HotSpot™ 64-Bit Server VM


java version "1.8.0_181"
Java™ SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot™ 64-Bit Server VM (build 25.181-b13, mixed mode)


The JVM was able to detect the container has only 1GB and set the max heap to 228M, which is almost 1/4th of the total available size.


We can see that the Java Heap is set to 1/4th of the docker size and rest of the 3/4th memory is utilized by docker, but what if our container doesn't require 3/4 memory,


If the container or the program configuration doesn't need that much of memory then this is not the correct utilization of memory. The ratio between Java heap and Container size is defined inside JVM code, so the question is - can we configure this ratio??


Yes, Java did a very good thing by providing another option to change the ratio of container memory and java heap size. Java introduced another flag as MaxRAMFraction ( the default value to this flag is 4, so heap size takes 1/4th of container size and we can change it by defining it explicitly)


we have defined RAM fraction =2, Which settles heap size on almost 50% of the total.
[root]# docker run -m 1GB oracle-server-jre java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=2 -XshowSettings:vm -version
VM settings:    
    Max. Heap Size (Estimated): 455.50M
    Ergonomics Machine Class: server
    Using VM: Java HotSpot™ 64-Bit Server VM


java version "1.8.0_181"
Java™ SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot™ 64-Bit Server VM (build 25.181-b13, mixed mode)


The heap size reaches approximately to 50% of the total container size.


Now we can change the ratio as well according to our Java program comfort level, but this leads to another thought that is is safe to run a container, if we define this ratio to 1? (however JVM won't capture whole container memory and It leaves some memory to run some other container programs like container debug shell (docker exec) and diagnostics, OS processes, etc. If the program or container needs more memory under load then it will kill the container, so I feel defining the least ratio-XX:MaxRAMFraction=2 seems safe(ish), if we want to customize the default JVM ratio which seems to be the safest ratio.




https://blogs.oracle.com/java/java-heap-...and-easily
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Oracle Blog] Devoxx 2019: Java, Java and more JAVA! xSicKxBot 0 74 02-01-2020, 02:30 PM
Last Post: xSicKxBot
  [Oracle Blog] 2019 Duke's Choice Award Winners! xSicKxBot 0 49 01-31-2020, 03:39 PM
Last Post: xSicKxBot
  [Oracle Blog] Java on Container Like A Pro xSicKxBot 0 51 01-30-2020, 06:27 PM
Last Post: xSicKxBot
  [Oracle Blog] Java and the New Duke Personality xSicKxBot 0 103 01-29-2020, 09:04 PM
Last Post: xSicKxBot
  [Oracle Blog] Java is alive at Oracle Code One! xSicKxBot 0 59 01-28-2020, 09:32 PM
Last Post: xSicKxBot
  [Oracle Blog] 2019 Duke's Choice Award Nominations xSicKxBot 0 77 01-28-2020, 02:23 AM
Last Post: xSicKxBot
  Bring joy to development with Quarkus, the cloud-native Java framework xSicKxBot 0 63 01-27-2020, 05:17 AM
Last Post: xSicKxBot
  [Oracle Blog] Java Magazine on Containers xSicKxBot 0 65 01-27-2020, 05:17 AM
Last Post: xSicKxBot
  [Oracle Blog] Java Magazine on Lightweight Frameworks xSicKxBot 0 64 01-26-2020, 09:15 AM
Last Post: xSicKxBot
  [Oracle Blog] 30 New Java Champions Confirmed in 2018 xSicKxBot 0 138 01-25-2020, 11:50 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Upcoming Events

[-]
Discord

[-]
Latest Threads
Shiro’s Game Technology Stack
Last Post: xSicKxBot
Today 12:19 AM
» Replies: 0
» Views: 0
News - Video: See The LEGO Mario Starter...
Last Post: xSicKxBot
Today 12:17 AM
» Replies: 0
» Views: 0
News - Guide: Where To Preorder The Tota...
Last Post: xSicKxBot
Today 12:17 AM
» Replies: 0
» Views: 0
News - Best Budget Monitors For Under $2...
Last Post: xSicKxBot
Today 12:17 AM
» Replies: 0
» Views: 0
[Tut] Python List Length – What’s the Ru...
Last Post: xSicKxBot
Yesterday 11:07 PM
» Replies: 0
» Views: 4
(Indie Deal) FREE Ring Runner: Flight of...
Last Post: xSicKxBot
Yesterday 11:07 PM
» Replies: 0
» Views: 0
Xbox Wire - Adventures in “Minecraft Ear...
Last Post: xSicKxBot
Yesterday 11:06 PM
» Replies: 0
» Views: 1
News - Blog: Best virtual development pr...
Last Post: xSicKxBot
Yesterday 11:06 PM
» Replies: 0
» Views: 1
News - Journey to the Savage Planet a re...
Last Post: xSicKxBot
Yesterday 11:06 PM
» Replies: 0
» Views: 1
News - Poll: Which ARMS Fighter Should J...
Last Post: xSicKxBot
Yesterday 05:22 PM
» Replies: 0
» Views: 4

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2019