Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to avoid MATLAB crash when opening too many figures?
    text
    copied!<p>Sometimes I start a MATLAB script and realize too late that it is going to output way too many figures. Eventually I get an </p> <blockquote> <p>Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space</p> </blockquote> <p>which can easily be reproduced on my machine using</p> <pre><code>for i=1:inf figure; end </code></pre> <p>I get to around ~90 figures before it crashes with the standard setting (<a href="http://www.mathworks.com/help/techdoc/matlab_env/brjh09y-1.html#bsemees-1" rel="noreferrer">Preferences / Java Heap Memory</a>) of 128 MB Java heap, while doubling the Heap to 256 MB gives me around 200 figures. </p> <p><em>Do you see any way to <strong>avoid</strong> the Java error message?</em> If there is not enough memory for another figure, I'd like my script to be <em>told</em> rather than crash. </p> <p>Maybe I could have a wrapper for <code>figure</code> which (somehow?) checks how much Java heap is available and which refuses to open a new figure if there is not enough space left?</p> <p><strong>Update</strong></p> <p>Using the answers below, I get a nice graph for how much free Memory Java has:</p> <p><img src="https://i.stack.imgur.com/EMnHC.png" alt="figure;plot(freeMem/1E6,&#39;x&#39;);ylabel(&#39;java.lang.Runtime.getRuntime.freeMemory [MB]&#39;);xlabel(&#39;number of empty figures created&#39;);"></p> <p>This was produced using </p> <pre><code>for i=1:inf java.lang.Runtime.getRuntime.gc fprintf('%3.0f: %1.0f Byte free\n',i,java.lang.Runtime.getRuntime.freeMemory); figure; end </code></pre> <p>I assume the increase in the beginning means that garbage collection only does a certain effort every time I call it?</p> <p><strong>Update 2 - my solution</strong></p> <p>Using the help I got here, I implemented the following solution as a <code>figure.m</code> which overloads and calls the build-in <code>figure</code> command:</p> <pre><code>function varargout=figure(varargin) memcutoff = 10E6; % keep at least this amount of bytes free memkeyboard= 3E6; % if memory drops below this, interrupt execution and go to keyboard mode global refuse_new_figures if refuse_new_figures warning('jb:fig:lowjavamem2','Java WAS memory low -&gt; refusing to create a new figure. To reset, type "global refuse_new_figures ; refuse_new_figures = [];"'); return end freemem=java.lang.Runtime.getRuntime.freeMemory; if freemem &lt; memcutoff fprintf('Free memory is low (%1.0f Bytes) -&gt; running garbace collector...\n',freemem); java.lang.Runtime.getRuntime.gc end freemem=java.lang.Runtime.getRuntime.freeMemory; % fprintf('Free memory is %1.0f Bytes.\n',freemem); if freemem &lt; memkeyboard warning('jb:fig:lowjavamem','Java memory very low -&gt; going into interactive mode. Good luck!'); keyboard; end if freemem &lt; memcutoff warning('jb:fig:lowjavamem','Java memory low -&gt; refusing to create a new figure!'); refuse_new_figures=true; else if nargin &gt; 0 if nargout &gt; 0 varargout{1}=builtin('figure',varargin{:}); else builtin('figure',varargin{:}); end else if nargout &gt; 0 varargout{1}=builtin('figure'); else builtin('figure'); end end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload