MATLAB Answers

Does Matlab have memory leaks?

92 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)

  0 Comments

Sign in to comment.

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

John Anderson
John Anderson on 2 Dec 2013
Hi Malcolm,
Thanks for replying. I will take a look at JConsole. I agree that garbage collection seems to take place when required and generally works pretty well, as you suggest. If I run the following function it is possible to observe garbage collection taking place, when required, using calls to java.lang.Runtime.getRuntime.freeMemory.
function testFigure
clear all;
f = figure( 'windowStyle', 'docked' );
n = 1000;
m = zeros( 1, n );
for i = 1 : n
h = figure;
drawnow;
close(h)
m( i ) = java.lang.Runtime.getRuntime.freeMemory;
figure( f );
plot( m );
drawnow;
end
clear all;
The changes in free memory are revealed as a saw toothed waveform as a function of figure number indicating restoration of the original total avilable memory every 20 figures or so.
However, if I execute a similar function instantiating a uitable then the sawtoothed memory profile gradually runs down.
function testFigure
clear all;
f = figure( 'windowStyle', 'docked' );
n = 1000;
m = zeros( 1, n );
for i = 1 : n
h = figure;
ht = uitable( h, 'data', rand( 10 ) );
delete( ht );
drawnow;
close(h)
m( i ) = java.lang.Runtime.getRuntime.freeMemory;
figure( f );
plot( m );
drawnow;
end
clear all;
When conducting reasonably graphic-intensive work the amount of available memory steadily decreases so that the sawtooth memory profile eventually runs down to zero free memory. It is at this point that matlab begins to become sluggish, eventually hanging, and then recently taking down the operating system.
In this case would it be best practise to simply keep an eye on the amount of free memory and force my code to stop executing when it gets critically low. I can then restart Matlab manually to avoid the risk of crashing the operating system? Or, are there better general house-keeping methods I should be applying to clean up memory during execution?
Any help appreciated,
Best wishes,
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)

Community Treasure Hunt

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

Start Hunting!