tags:

views:

307

answers:

2

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?

A: 

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?

dwj
+5  A: 

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

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

Jonas
thanks! My problem was exactly #2
noam
+1. For case 3, you shouldn't need a reboot. The fragmentation occurs in the virtual memory space allocated to the Matlab process; restarting Matlab is sufficient to get a clean slate.
Andrew Janke
Thanks for the clarification. If I recall correctly, on Windows NT (or was it Win95?), we had to reboot completely. I'm glad that there is progress in this world!
Jonas