{"id":1582,"date":"2022-09-30T16:05:33","date_gmt":"2022-09-30T03:05:33","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=1582"},"modified":"2022-09-30T16:11:11","modified_gmt":"2022-09-30T03:11:11","slug":"container-aware-jvm-parameters-for-heap-memory","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=1582","title":{"rendered":"Container Aware JVM Parameters for Heap Memory"},"content":{"rendered":"<h2>JVM Parameters<\/h2>\n<p>Modify the java heap memory based on container memory using one of the following JVM parameters:<\/p>\n<table>\n<thead>\n<tr>\n<th>Parameter<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>-XX:InitialRAMPercentage<\/td>\n<td>The <strong>initial size of the heap based<\/strong> on the total container memory.<\/td>\n<\/tr>\n<tr>\n<td>-XX:MinRAMPercentage<\/td>\n<td>The <strong>maximum heap size based on the size<\/strong> of the JVM running on <strong>small heap<\/strong>. The small is heap is of approximately 125MB.<\/td>\n<\/tr>\n<tr>\n<td>-XX:MaxRAMPercentage<\/td>\n<td>The <strong>maximum heap size based on the size<\/strong> of the JVM running on greater than small heap.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Displaying the Current Value<\/h2>\n<p>Use the following command the display the current value of the container aware JVM Parameters using the following:<\/p>\n<pre><code>docker container run -it --rm openjdk:17.0.2-slim java -XX:+PrintFlagsFinal -version | grep -E &quot;.*RAMPercentage&quot;<\/code><\/pre>\n<p>Expect to see something like the following:<\/p>\n<pre><code>   double InitialRAMPercentage                     = 1.562500\n                   {product} {default}\n   double MaxRAMPercentage                         = 25.000000\n                   {product} {default}\n   double MinRAMPercentage                         = 50.000000\n                   {product} {default}<\/code><\/pre>\n<h2>Checking the Current Memory and CPU using JShell<\/h2>\n<ol>\n<li>\n<p><strong>Open a jshell in the container<\/strong> using the following:<\/p>\n<pre><code>docker container run -it --rm openjdk:17.0.2-slim jshell<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Paste the following command<\/strong> in jshell:<\/p>\n<pre><code class=\"language-java\">var rt = Runtime.getRuntime();\nSystem.out.printf(\"Heap size: %dMB%nMaximum size of heap: %dMB%nAvailable processors: %d%n\", \n rt.totalMemory()\/1024\/1024, rt.maxMemory()\/1024\/1024, rt.availableProcessors());<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Press enter<\/strong>.<\/p>\n<blockquote>\n<p>Expect to see something similar to the following:<\/p>\n<pre><code>rt ==> java.lang.Runtime@1d81eb93\nHeap size: 252MB\nMaximum size of heap: 3966MB\nAvailable processors: 16\n$2 ==> java.io.PrintStream@34c45dca<\/code><\/pre>\n<\/blockquote>\n<\/li>\n<li>\n<p><strong>Type the following<\/strong> in jshell and <strong>press enter<\/strong>.<\/p>\n<pre><code>\/exit<\/code><\/pre>\n<\/li>\n<\/ol>\n<h2>Allocating a Memory and CPU to a Container<\/h2>\n<ol>\n<li>\n<p><strong>Execute the following command<\/strong> to allocate <strong>100mb of memory<\/strong> and <strong>1 CPU<\/strong> to the container:<\/p>\n<pre><code>docker container run -it --rm -m 100m --cpus=1 openjdk:17.0.2-slim jshell<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Paste the following command<\/strong> in jshell:<\/p>\n<pre><code class=\"language-java\">var rt = Runtime.getRuntime();\nSystem.out.printf(\"Heap size: %dMB%nMaximum size of heap: %dMB%nAvailable processors: %d%n\", \n rt.totalMemory()\/1024\/1024, rt.maxMemory()\/1024\/1024, rt.availableProcessors());<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Press enter<\/strong>.<\/p>\n<blockquote>\n<p>Expect to see something similar to the following:<\/p>\n<pre><code>rt ==> java.lang.Runtime@6659c656\nHeap size: 7MB\nMaximum size of heap: 48MB\nAvailable processors: 1\n$2 ==> java.io.PrintStream@2d8e6db6<\/code><\/pre>\n<p>Notice the following:<\/p>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th>Value<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Heap size<\/td>\n<td>7MB<\/td>\n<\/tr>\n<tr>\n<td>Maximum size of heap<\/td>\n<td>48MB<\/td>\n<\/tr>\n<tr>\n<td>Available processors<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/blockquote>\n<\/li>\n<li>\n<p><strong>Type the following<\/strong> in jshell and <strong>press enter<\/strong>.<\/p>\n<pre><code>\/exit<\/code><\/pre>\n<\/li>\n<\/ol>\n<h2>Modifying the Allocated Maximum Size of Heap Memory<\/h2>\n<ol>\n<li>\n<p><strong>Execute the following command<\/strong> to allocate <strong>100mb of memory<\/strong> and <strong>1 CPU<\/strong> to the container:<\/p>\n<pre><code>docker container run -it --rm -m 100m --cpus=1 openjdk:17.0.2-slim jshell -R-XX:MinRAMPercentage=80<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Paste the following command<\/strong> in jshell:<\/p>\n<pre><code class=\"language-java\">var rt = Runtime.getRuntime();\nSystem.out.printf(\"Heap size: %dMB%nMaximum size of heap: %dMB%nAvailable processors: %d%n\", \n rt.totalMemory()\/1024\/1024, rt.maxMemory()\/1024\/1024, rt.availableProcessors());<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Press enter<\/strong>.<\/p>\n<blockquote>\n<p>Expect to see something similar to the following:<\/p>\n<pre><code>rt ==> java.lang.Runtime@6659c656\nHeap size: 7MB\nMaximum size of heap: 77MB\nAvailable processors: 1\n$2 ==> java.io.PrintStream@2d8e6db6<\/code><\/pre>\n<p>Notice the <strong>Maximum size of heap<\/strong> become <strong>77MB<\/strong>. This is because of the JVM argument <strong>-XX:MinRAMPercentage=80<\/strong> passed in jshell as:<\/p>\n<pre><code>-R-XX:MinRAMPercentage=80<\/code><\/pre>\n<p>We use the --XX:MinRAMPercentage=80 because the memory allocated is a small heap.<\/p>\n<\/blockquote>\n<\/li>\n<li>\n<p><strong>Type the following<\/strong> in jshell and <strong>press enter<\/strong>.<\/p>\n<pre><code>\/exit<\/code><\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>JVM Parameters Modify the java heap memory based on container memory using one of the following JVM parameters: Parameter Description -XX:InitialRAMPercentage The initial size of the heap based on the total container memory. -XX:MinRAMPercentage The maximum heap size based on the size of the JVM running on small heap. The small is heap is of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[78,74],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1582"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1582"}],"version-history":[{"count":3,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1582\/revisions"}],"predecessor-version":[{"id":1585,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1582\/revisions\/1585"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}