开发者

memory not freed in matlab?

开发者 https://www.devze.com 2022-12-23 15:46 出处:网络
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.

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?开发者_Go百科


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


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


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.


It sounds like you are not clearing any of your variables. You should either provide a way to stop the loop without hitting ctrl-c (write a simple GUI with a "Stop" button and your display) and then clean up your workspace in the script or clear your variables at the start of the script.

Are you intentionally storing all the data (or some large component) on each iteration of your loop?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号