Java outofmemoryerror while saving figures

4 ビュー (過去 30 日間)
Mike
Mike 2011 年 7 月 22 日
コメント済み: Marwan ALKHEIR 2019 年 2 月 11 日
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
...
... % figure plotting
saveas(gcf, figure_name);
close all;
end
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(FileLocation.java:333)
at com.mathworks.mlwidgets.explorer.model.table.PathAffordance.currentDirectoryIsStillTheSame(PathAffordance.java:244)
at com.mathworks.mlwidgets.explorer.model.table.PathAffordance.getDisplayEffects(PathAffordance.java:270)
at com.mathworks.widgets.grouptable.AffordanceManager.updateCacheForVisibleItems(AffordanceManager.java:225)
at com.mathworks.widgets.grouptable.AffordanceManager.access$000(AffordanceManager.java:32)
at com.mathworks.widgets.grouptable.AffordanceManager$1.run(AffordanceManager.java:50)
at com.mathworks.util.RequestFilter$Request.run(RequestFilter.java:112)
at com.mathworks.util.RequestFilter$1$1.run(RequestFilter.java:54)
at com.mathworks.util.RequestAggregator$3.run(RequestAggregator.java:263)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
Exception in thread "RequestQueue for GenericFileList" java.lang.OutOfMemoryError: Java heap space
at com.mathworks.mlwidgets.explorer.model.realfs.StatToEntryAdapter.receive(StatToEntryAdapter.java:44)
at com.mathworks.mlwidgets.explorer.model.realfs.StatToEntryAdapter.receive(StatToEntryAdapter.java:14)
at com.mathworks.util.NativeJava.findFilesWindows(Native Method)
at com.mathworks.mlwidgets.explorer.model.realfs.RealFileList.readFolders(RealFileList.java:57)
at com.mathworks.mlwidgets.explorer.model.genericfs.GenericFileList$7.run(GenericFileList.java:503)
at com.mathworks.util.RequestQueue.execute(RequestQueue.java:87)
at com.mathworks.util.RequestQueue.access$000(RequestQueue.java:22)
at com.mathworks.util.RequestQueue$1.run(RequestQueue.java:58)
at java.lang.Thread.run(Unknown 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(DTClient.java:1735)
at com.mathworks.widgets.desk.Desktop.saveLayout(Desktop.java:4677)
at com.mathworks.widgets.desk.Desktop.saveLayout(Desktop.java:4586)
at com.mathworks.widgets.desk.Desktop$38.call(Desktop.java:3684)
at com.mathworks.widgets.desk.Desktop$38.call(Desktop.java:3672)
at com.mathworks.widgets.desk.Desktop.deferredCall(Desktop.java:7135)
at com.mathworks.widgets.desk.Desktop.saveLayout(Desktop.java:3672)
at com.mathworks.widgets.desk.DTLayoutSaveManager.performSave(DTLayoutSaveManager.java:127)
at com.mathworks.widgets.desk.DTLayoutSaveManager.access$500(DTLayoutSaveManager.java:18)
at com.mathworks.widgets.desk.DTLayoutSaveManager$1.actionPerformed(DTLayoutSaveManager.java:47)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown 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 java.awt.EventDispatchThread.run(Unknown 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 件のコメント
Marwan ALKHEIR
Marwan ALKHEIR 2019 年 2 月 11 日
Bonjour,
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.

サインインしてコメントする。

回答 (2 件)

Giovanni
Giovanni 2011 年 7 月 22 日
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 件のコメント
Mike
Mike 2011 年 7 月 22 日
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.

サインインしてコメントする。


Malcolm Lidierth
Malcolm Lidierth 2013 年 11 月 17 日
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.

カテゴリ

Help Center および File ExchangeJava Package Integration についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by