MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
c = rand(2.3e6,1);
clear
a = rand(1e6,1);
b = rand(1e6,1);

Long-Term Usage (Windows Systems Only)

On 32-bit Microsoft Windows, the workspace of MATLAB can fragment over time due to
the fact that the Windows memory manager does not return blocks of certain types and
sizes to the operating system. Clearing the MATLAB workspace does not fix this problem.
You can minimize the problem by allocating the largest variables first. This cannot
address, however, the eventual fragmentation of the workspace that occurs from
continual use of MATLAB over many days and weeks, for example. The only solution to
this is to save your work and restart MATLAB.

The pack command, which saves all variables to disk and loads them back, does not help
with this situation.

Reclaiming Used Memory


One simple way to increase the amount of memory you have available is to clear large
arrays that you no longer use.

Save Your Large Data Periodically to Disk

If your program generates very large amounts of data, consider writing the data to disk
periodically. After saving that portion of the data, use the clear function to remove the
variable from memory and continue with the data generation.

Clear Old Variables from Memory When No Longer Needed

When you are working with a very large data set repeatedly or interactively, clear the old
variable first to make space for the new variable. Otherwise, MATLAB requires temporary
storage of equal size before overriding the variable. For example,

a = rand(100e6,1) % 800 MB array
b = rand(100e6,1) % New 800 MB array
Error using rand
Out of memory. Type HELP MEMORY for your options.

clear a
a = rand(100e6,1) % New 800 MB array

29 Memory Usage

Free download pdf