Java outofmemoryerror while saving figures

9 views (last 30 days)
Mike on 22 Jul 2011
Commented: Marwan ALKHEIR on 11 Feb 2019
I have thousands of .mat files, and for each mat file, I need to make a plot and save it to .png figure. In this big loop, I am using the following script to generate figure and saveas them.
for ii=1:N
... % figure plotting
saveas(gcf, figure_name);
close all;
It took roughly 1 sec to generate and save one figure. This script has run for several hours without showing any problems. Then the following error occured:
Exception in thread "Timer-8614" java.lang.OutOfMemoryError: Java heap space
at com.mathworks.matlab.api.explorer.FileLocation.equals(
at com.mathworks.mlwidgets.explorer.model.table.PathAffordance.currentDirectoryIsStillTheSame(
at com.mathworks.mlwidgets.explorer.model.table.PathAffordance.getDisplayEffects(
at com.mathworks.widgets.grouptable.AffordanceManager.updateCacheForVisibleItems(
at com.mathworks.widgets.grouptable.AffordanceManager.access$000(
at com.mathworks.widgets.grouptable.AffordanceManager$
at com.mathworks.util.RequestFilter$
at com.mathworks.util.RequestFilter$1$
at com.mathworks.util.RequestAggregator$
at java.util.TimerThread.mainLoop(Unknown Source)
at Source)
Exception in thread "RequestQueue for GenericFileList" java.lang.OutOfMemoryError: Java heap space
at com.mathworks.mlwidgets.explorer.model.realfs.StatToEntryAdapter.receive(
at com.mathworks.mlwidgets.explorer.model.realfs.StatToEntryAdapter.receive(
at com.mathworks.util.NativeJava.findFilesWindows(Native Method)
at com.mathworks.mlwidgets.explorer.model.realfs.RealFileList.readFolders(
at com.mathworks.mlwidgets.explorer.model.genericfs.GenericFileList$
at com.mathworks.util.RequestQueue.execute(
at com.mathworks.util.RequestQueue.access$000(
at com.mathworks.util.RequestQueue$
at Source)
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at org.apache.xerces.dom.CoreDocumentImpl.createAttribute(Unknown Source)
at org.apache.xerces.dom.ElementImpl.setAttribute(Unknown Source)
at com.mathworks.widgets.desk.DTClient.toXML(
at com.mathworks.widgets.desk.Desktop.saveLayout(
at com.mathworks.widgets.desk.Desktop.saveLayout(
at com.mathworks.widgets.desk.Desktop$
at com.mathworks.widgets.desk.Desktop$
at com.mathworks.widgets.desk.Desktop.deferredCall(
at com.mathworks.widgets.desk.Desktop.saveLayout(
at com.mathworks.widgets.desk.DTLayoutSaveManager.performSave(
at com.mathworks.widgets.desk.DTLayoutSaveManager.access$500(
at com.mathworks.widgets.desk.DTLayoutSaveManager$1.actionPerformed(
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$ Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at Source)
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Timer-2" Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
??? Error using ==> close>request_close at 186
Java exception occurred:
* java.lang.OutOfMemoryError: Java heap space*
Error in ==> close at 100
status = request_close(h);
Error in ==> get_jpdf at 133
close all;
Caused by:
Error while evaluating figure CloseRequestFcn
I caught the words "java.lang.OutOfMemoryError: Java heap space", it is a out-of-memory error. So I figure, although I close the figure I generate in each loop, maybe it still stays somewhere in the memory. So after long enough running time, they pile up in the memory and give such errors.
I am not sure if my guess is correct or not. Can anyone give me a little help? Thank you.
  1 Comment
Marwan ALKHEIR on 11 Feb 2019
J'avais le même problème, c'était un problème de résolution de l'écran, car j'étais sur un écran et j'ai changé l'écran en extension, il faut redémarrer le pc et relancer le code de nouveau, si vous tombez sur le même problème il faut changer le nom d'enregistrement.

Sign in to comment.

Answers (2)

Giovanni on 22 Jul 2011
Hi, I am not sure how Matlab and java work together, but I think java is mostly used to create and save plots. I have experienced this error before when trying to save an image with too high resolution, so you might want to check that. That said, I don't think there's an easy way around the problem. I don't think there's a way to call the java garbage collector from Matlab, but you can free workspace memory using the clear varname command and that might help. If you have axis or figure handles in your code, you might want to clear them. At this point, since you need to be efficient, you should probably use the clear command as much as you can at the end of your loop to clean up data you don't need in the following iteration to avoid memory leaks. I hope that helps. Good luck!
  1 Comment
Mike on 22 Jul 2011
That is a good idea. I will do that when I rerun it.
I am still wondering if Java is sharing memory with matlab, or it has a separate block of memory, which command like clear all won't affect....
In my code, in each loop, all variables are given new values. So I figure if there is a problem with memory, it should happen in the first few loops. It won't wait to be out of memory after a few thousand loops.

Sign in to comment.

Malcolm Lidierth
Malcolm Lidierth on 17 Nov 2013
It looks like the save to file is running on the Java Event Despatch Thread (AWT-EventQueue-0)- so Java references will persist on the EDT after your call to "close" on the figure. That could be a cause of the "leaks". Invoking the JVM GC will not help if references still exist to the objects.
A drawnow() call after each saveas should cause the MATLAB thread to wait until the file write is completed on each iteration.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!