{"id":993,"date":"2018-09-17T19:06:49","date_gmt":"2018-09-17T10:06:49","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=993"},"modified":"2019-06-30T18:44:15","modified_gmt":"2019-06-30T09:44:15","slug":"runtime-totalmemory-freememory","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/en\/blog\/java\/runtime-totalmemory-freemememory","title":{"rendered":"Get Java memory usage"},"content":{"rendered":"<h2 class=\"common_title\"><a name=\"0\">Java <strong>memory<\/strong>Get usage <strong>Runtime.getRuntime().totalMemory, freeMemory<\/strong><\/a><\/h2>\n<p>\n<strong>Java <\/strong> Returns \"total,\" \"used,\" and \"maximum available\" memory information for the virtual machine.<br \/>\nThe description of each item is as follows\n<\/p>\n<ul>\n<li><strong>total amount<\/strong>...<strong>Runtime.getRuntime().totalMemory()<\/strong>and is the amount of memory allocated to the Java virtual machine.<\/li>\n<li><strong>amount used<\/strong>...<strong>Runtime.getRuntime().totalMemory()<\/strong>\u3000-<strong>Runtime.getRuntime().freeMemory()<\/strong>become,<br \/>\nMemory usage of objects currently allocated in memory.<\/li>\n<li><strong>maximum usable<\/strong>...<strong>Runtime.getRuntime().maxMemory()<\/strong>and is the maximum amount of memory that the Java virtual machine will attempt to use.<br \/>\nIf usage approaches the total and garbage collection does not free memory, the Java Virtual Machine expands to the \"maximum available\".<\/li>\n<\/ul>\n<h2 class=\"common_title\"><a name=\"1\">sample program<\/a><\/h2>\n<pre class=\"sample_src\">\/**\r\n * Total memory capacity and usage of the Java Virtual Machine,\r\n * Returns information about the maximum amount of memory to attempt to use.\r\n * @return Java Virtual Machine memory information.\r\n *\/\r\npublic static String getMemoryInfo() {\r\n    DecimalFormat f1 = new DecimalFormat(\"#,###KB\");\r\n    DecimalFormat f2 = new DecimalFormat(\"##.#\");\r\n    long free = Runtime.getRuntime().freeMemory() \/ 1024;\r\n    long total = Runtime.getRuntime().totalMemory() \/ 1024;\r\n    long max = Runtime.getRuntime().maxMemory() \/ 1024;\r\n    long used = total - free;\r\n    double ratio = (used * 100 \/ (double)total);\r\n    String info =\r\n    \"Java memory information : total = \" + f1.format(total) + \",\" +\r\n    \"Used = \" + f1.format(used) + \" (\" + f2.format(ratio) + \"%),\" +\r\n    \"Maximum available = \" + f1.format(max);\" + f1.format(max)\r\n    return info; }\r\n}\r\n<\/pre>\n<p><script type=\"text\/javascript\" src=\"https:\/\/chat-messenger.com\/js\/common.js?dd12sssas2223\" charset=\"UTF-8\"><\/script><br \/>\n<script type=\"text\/javascript\"> writePR(); <\/script><\/p>\n<h2 class=\"common_title\"><a name=\"2\">Execution result 1<\/a><\/h2>\n<p>\u25c6Example of Execution<\/p>\n<pre class=\"sample_src\">public static void main(String[] args) {\r\n    System.out.println(getMemoryInfo());\r\n}\r\n<\/pre>\n<p>\u25c6Output result<\/p>\n<pre class=\"console\">Java memory information : Total=1,984KB, usage=458KB (23.1%), maximum available=65,088KB\r\n<\/pre>\n<h2 class=\"common_title\"><a name=\"3\">Execution Result 2<\/a><\/h2>\n<p>\u25c6Example of Execution<br \/>\nChange the Java heap size and run it.<br \/>\nThe Java heap size value can be specified in the Java command options.<\/p>\n<div class=\"point\">\njava -Xms64m -Xmx512m Main <\/p>\n<p>-Xms initial heap size<br \/>\nSpecify the amount of initial memory allocation for the Java virtual machine. The default is 2 MB.<br \/>\n-Xmx maximum heap size<br \/>\nSpecify the maximum memory allocation for the Java virtual machine. Default is 64 MB.\n<\/div>\n\n<pre class=\"sample_src\">public static void main(String[] args) {\r\n    System.out.println(getMemoryInfo());\r\n}\r\n<\/pre>\n<p>\u25c6Output result<\/p>\n<pre class=\"console\">Java memory information : Total=65,088KB, usage=524KB (0.8%), maximum available=520,256KB\r\n<\/pre>\n<p>Total Runtime.getRuntime().totalMemory()\" is<strong>-Xms<\/strong>and \"maximum available Runtime.getRuntime().maxMemory()\" in the<strong>-Xmx<\/strong>Link to the<\/p>","protected":false},"excerpt":{"rendered":"<p>Get Java memory usage Runtime.getRuntime().totalMemory, freeMe [\u2026]<\/p>","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/993"}],"collection":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/comments?post=993"}],"version-history":[{"count":9,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/993\/revisions"}],"predecessor-version":[{"id":3636,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/993\/revisions\/3636"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/media?parent=993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/categories?post=993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/tags?post=993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}