{"id":375,"date":"2010-04-26T14:22:19","date_gmt":"2010-04-26T14:22:19","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2010\/04\/26\/controlling-the-java-heap-size\/"},"modified":"2010-04-26T14:22:19","modified_gmt":"2010-04-26T14:22:19","slug":"controlling-the-java-heap-size","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2010\/04\/26\/controlling-the-java-heap-size\/","title":{"rendered":"Controlling the Java Heap Size"},"content":{"rendered":"<p>When you create a Java object from MATLAB, that object will live in the Java heap, whereas workspace variables will go into MATLAB&#8217;s main memory. The Java heap space is also shared with MATLAB user interface components, such as figures, the Desktop, and the Editor. You can see in the following example that no size is listed for the java variable, <tt>ja<\/tt>:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">free = java.lang.Runtime.getRuntime.freeMemory\r\nja(1,1,1) = java.lang.Double(23);\r\nwhos\r\nfree = java.lang.Runtime.getRuntime.freeMemory<\/pre>\n<pre style=\"font-style:oblique\">\r\nfree =\r\n\r\n    33760000\r\n\r\n  Name      Size            Bytes  Class                 Attributes\r\n\r\n  free      1x1                 8  double                          \r\n  ja        1x1                    java.lang.Double[]              \r\n\r\n\r\nfree =\r\n\r\n    33736872\r\n\r\n<\/pre>\n<p>Total amount of memory available for Java is set when MATLAB starts up and cannot be changed at run time. However this value can be queried with the following command:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">max = java.lang.Runtime.getRuntime.maxMemory<\/pre>\n<pre style=\"font-style:oblique\">\r\nmax =\r\n\r\n   130875392\r\n\r\n<\/pre>\n<p>I <a href=\"https:\/\/blogs.mathworks.com\/community\/2009\/08\/17\/calling-java-from-matlab-memory-issues\/\">wrote awhile ago<\/a> about how MATLAB manages its Java memory (including the distinction between the maximum heap size, the current heap size, and free memory). At the time (MATLAB R2009a) the only way to change the maximum value by creating a <tt>java.opts<\/tt> file in your startup directory to set this value with a JVM flag. In the instances where you need more available memory (generally when dealing with Java-intensive toolboxes or managing a lot of really large files), we&#8217;ve made it easier in MATLAB R2010a to set this value with panel in the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/matlab_env\/brjh09y-1.html#bsemees-1\">Preferences<\/a>: File -> Preferences -> General -> Java Heap Memory. No longer do you have to worry about setting JVM flag directly.<\/p>\n<div align=\"center\">\n<img decoding=\"async\" border=\"0\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_java_memory\/java_heap_prefs.png\" alt=\"MATLAB Java Heap Size Preferences\">\n<\/div>\n<p>If you should run out of Java memory, just come to this dialog, and move the slider to the right. You&#8217;ll then have to restart MATLAB for the settings to take effect. Note that the default and maximum heap settings vary by platform and processor word-size. <\/p>\n<p>I don&#8217;t know the demographics of my readers (beyond the general MATLAB user), but if you&#8217;re of my generation, maybe you remember Fraggle Rock? Whenever I hear the word heap, I can&#8217;t help but think of the show&#8217;s Trash Heap. If you&#8217;ve got two minutes to waste, here&#8217;s a <a href=\"http:\/\/www.youtube.com\/watch?v=04Zz-GRPAzA\">YouTube video<\/a> of said Heap.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you create a Java object from MATLAB, that object will live in the Java heap, whereas workspace variables will go into MATLAB&#8217;s main memory. The Java heap space is also shared with MATLAB&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2010\/04\/26\/controlling-the-java-heap-size\/\">read more >><\/a><\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/375"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=375"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/375\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}