Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memory not freed in matlab?

Tags:

memory

matlab

I am running a script that animates a plot (simulation of a water flow). After a while, I kill the loop by doing ctrl-c. After doing this several times I get the error:

??? Error: Out of memory.

And after I start receiving that error, every call to my script will generate it.
Now, it happens before anything inside the function that I am calling is executed, i.e even if I add the line a=1 as the first line of the function I am calling, I still get the error and no printout, so the code inside the function doesn't even get executed. What could be causing this?

like image 590
olamundo Avatar asked Mar 22 '10 23:03

olamundo


3 Answers

There are several possible reasons.

  1. Most likely your script creates some variables that are filling up the memory. Run

    clear all
    

    before restarting the script, so that all the variables are cleared, or change your script to a function (which will automatically erase all temporary variables after the function returns). Note that this also clears all loaded functions, so your next execution of the script has to load them again which will slow down the next execution by a (usually tiny) bit. It may be sufficient to call clear only.

  2. Maybe you're animating by plotting several plots over one another (without clearing the axes first). Thus you might run out of Java heap space. You can close the open figures individually, or run

    close all
    

    You can also increase the amount of Java Memory Matlab uses on your system (see instructions here) - note that the limit is generally rather low, annoyingly so if you want to tons of figures.

  3. Especially if you're running an older version of Windows, you may get your memory fragmented. Matlab needs contiguous blocks of free space to assign variables. To check for memory fragmentation, run

    memory
    

    and look at the number for the maximum possible variable size. If this is much smaller than the size available for all arrays, it's time to restart Matlab (I guess if you use a Windows version that would require a reboot to fix the problem, you may want to look into getting a new computer with Win7).

like image 118
Jonas Avatar answered Nov 18 '22 15:11

Jonas


You can also try the pack command, eg:

close all;
clear all;
pack;

to clear memory. Although after a recent mathworks seminar I asked one of the mathworks guru's and he also conformed @Andrew Janke's comment regarding memory fragmentation. Usually quitting and restarting matlab sorts this out for me (on XP).

like image 30
mor22 Avatar answered Nov 18 '22 17:11

mor22


clear all close all are straight-forward ways to free memory, which are known by all non-beginners.

The main issue is that when you have done some data large data processing, and cleared/closed everything off - there is still significant memory used by matlab.

This is a currently major problem with matlab, and to my knowledge there is no solution rather than restarting matlab, which is a pity.

like image 4
S C Avatar answered Nov 18 '22 15:11

S C