Does Matlab have memory leaks?

13 views (last 30 days)
John Anderson
John Anderson on 28 Nov 2013
Commented: Joe on 14 Sep 2015
I am experiencing intermittent crashes when I use Matlab that appear to be due to memory leaks. These are usually accompanied by java warnings (AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space) concerning available memory. Recently, when Matlab crashed it simultaneously took down Windows (blue screen of death). I have tried increasing the size of the Java heap memory via the preferences option (2012b) and this has helped reduced the frequency of the crashing. However, I am reluctant to leave Matlab running for long periods (>24 hours) on a problem and this is frustrating. To provide a simple example, if I run the function below and check the size of memory occupied by Matlab using task manager before and after execution I find an increase of around 30MB from 195,960K to 225,048K. It does not seem possible to recover this memory without restarting Matlab.
function testFigure
clear all;
for i = 1 : 1000
h = figure;
drawnow;
close(h)
end
clear all;
I am running the version of Matlab below
------------------------------------------------------------------------------------------------ MATLAB Version: 8.0.0.783 (R2012b) MATLAB License Number: **** Operating System: Microsoft Windows 7 Version 6.1 (Build 7601: Service Pack 1) Java Version: Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot™ Client VM mixed mode ------------------------------------------------------------------------------------------------ MATLAB Version 8.0 (R2012b) Simulink Version 8.0 (R2012b) Communications System Toolbox Version 5.3 (R2012b) Control System Toolbox Version 9.4 (R2012b) DSP System Toolbox Version 8.3 (R2012b) Datafeed Toolbox Version 4.4 (R2012b) Embedded Coder Version 6.3 (R2012b) Financial Instruments Toolbox Version 1.0 (R2012b) Financial Toolbox Version 5.0 (R2012b) Fixed-Point Toolbox Version 3.6 (R2012b) Fuzzy Logic Toolbox Version 2.2.16 (R2012b) Image Acquisition Toolbox Version 4.4 (R2012b) Image Processing Toolbox Version 8.1 (R2012b) Instrument Control Toolbox Version 3.2 (R2012b) MATLAB Coder Version 2.3 (R2012b) MATLAB Compiler Version 4.18 (R2012b) Neural Network Toolbox Version 8.0 (R2012b) Optimization Toolbox Version 6.2.1 (R2012b) Partial Differential Equation Toolbox Version 1.1 (R2012b) Signal Processing Toolbox Version 6.18 (R2012b) Simulink Coder Version 8.3 (R2012b) Simulink Control Design Version 3.6 (R2012b) Statistics Toolbox Version 8.1 (R2012b) Symbolic Math Toolbox Version 5.9 (R2012b)

Accepted Answer

Malcolm Lidierth
Malcolm Lidierth on 29 Nov 2013
Garbage collection is done when required, rather than when possible - so the simple loop is not really testing much.
Also MATLAB memory and Java Heap Space are different things.
For the main Q: Does Matlab have memory leaks? A: Yes
Try attaching JConsole, which is included in the JDK to view JVM memory while running MATLAB.
  3 Comments
Malcolm Lidierth
Malcolm Lidierth on 8 Jan 2014
I'd suggest reusing as many resources as possible in each iteration (h and ht) instead of deleting them.
Joe
Joe on 14 Sep 2015
I also was having the issue where the code failed because of the Java Heap issue.
Based on your comments, I was able to figure out that my problem was coming from
1) Creating a figure 2) Closing the figure 3) repeating this >6000 times.
Once I did what Malcolm suggested, that is just reusing the figure (only creating it once) my Java Heap issues disappeared.

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!