Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I fix out-of-memory condition?

69 views
Skip to first unread message

Scott Traurig

unread,
Nov 1, 1994, 3:31:53 PM11/1/94
to
Hi folks,

I am experiencing out of memory errors in Matlab. I am using 4.2a
running on a Sparc 10 under SunOS 4.1.3.

My Matlab program loops and runs for a very long time (on purpose).
This seems to be causing heap or memory fragmentation. It has been
suggested that I use the PACK command, but this command will not work
inside a loop.

Does anyone have any suggestions? Please e-mail, because I do not
subscribe to this group (Scott very kindly posted this message for me!)

Thanks,
Greg Laste (gla...@mailgw.sanders.lockheed.com)

c/o stra...@mailgw.sanders.lockheed.com

Bill York

unread,
Nov 3, 1994, 12:50:26 PM11/3/94
to
> I am experiencing out of memory errors in Matlab. I am using 4.2a
>running on a Sparc 10 under SunOS 4.1.3.

What follows is a bit of information compiled by our Technical Support
staff to help our customers understand why they receive the "Out of
Memory" message and possibly how to avoid it.

Let me know if this helps.

Bill

Memory management in MATLAB
---------------------------

- Topics common to all versions of MATLAB:

o There are five functions to improve the way in which MATLAB
handles memory: CLEAR, PACK, SAVE, QUIT, and LOAD.

CLEAR - removes variables from memory
PACK - writes existing variables off to disk, then reloads
them contiguously
QUIT - exits MATLAB and returns all allocated memory to the
system

Also, these two commands can be used to store data from one session
the next:

SAVE - can be used to selectively store variables to disk
LOAD - reloads a data file saved with the SAVE command

For more information on the above commands please refer to the
MATLAB Reference Guide.

o The amount of memory used by calling a function in this manner:

result=function2(function1(input99));

is the same as making two separate calls, like so:

result=function1(input99);
result=function2(result);

o MATLAB creates a list of M/MEX-file names at startup for all files
that reside below the directory found in the environment variable
$TOOLBOX. This list is stored in memory and is freed only when a new
list is to replace it during a MATLABPATH function call. M-file
pcode and MEX-file relocatable code is loaded into memory when the
function is called. The pcode or relocatable code is removed from
memory when:

- the function is called again and a new version now exists

- the function is explicitly cleared with the CLEAR command

- all functions are explicitly cleared with the CLEAR command

- MATLAB runs out of memory

o Declaring variables as global merely puts a flag in a symbol table
and does not use any more memory than defining non-global variables.
Consider the following example:

>> a = 5;
>> global a;

now there is one copy of "a" stored in the MATLAB workspace

>> clear a

now "a" has been removed from the MATLAB workspace, but it still
exists in the global workspace

>> clear global a

now "a" has been removed from the global workspace.

o Memory is allocated for variables whenever there is an '=' in the
statement. The statement 'x = 10' will allocate memory, but the
statement 'x(10) = 1' will not allocate memory if the 10th element
of x exists.

- Microsoft Windows specific topics:

o There are no functions implemented to manipulate the way MATLAB
handles Windows system resources. System resources are use by the
Windows operating system to track fonts, windows, and screen
objects. They can be depleted by using multiple figure windows,
multiple fonts, or several UI-controls. The best way to free up
system resources is to close all inactive windows - not iconify
them.

o The performance of a permanent swap file is typically better than
that of a temporary swap file.

o Typically a swap file twice the size of the installed RAM is
sufficient.

- Unix specific topics:

o Memory which MATLAB requests from the operating system will not be
returned to the operating system until the MATLAB process in
finished.

o MATLAB requests memory from the operating system when there is not
enough memory available in the MATLAB heap to store the current
variables and will reuse memory in the heap as long as the size of the
memory segment required is available in the MATLAB heap.

For example, on a Sun Sparc 10 running version MATLAB 4.2:

a = rand(1e6,1);
b = rand(1e6,1);

will use approximately 15.4 megs of RAM. And

c = rand(2.1e6,1);

will use approximately 16.4 megs of RAM. While

a = rand(1e6,1);
b = rand(1e6,1);
clear
c = rand(2.1e6,1);

will use approximately 32.4 megs of RAM.

This is because MATLAB was was not able to fit a 2.1 Meg array in the
space previously occupied by the two 1 Meg arrays. The simplest way
to prevent over allocation of memory, is to pre-allocate the largest
vector. Consider the following examples:

This:

a = rand(1e6,1);
b = rand(1e6,1);
clear
c = rand(2.1e6,1);

will use approximately 32.4 megs of RAM, and

c = rand(2.1e6,1);
clear
a = rand(1e6,1);
b = rand(1e6,1);

will use only about 16.4 megs of RAM. Clearly allocating the
largest vectors first will allow for optimal usage of the available
memory.

- What does "Out of Memory" mean?

Typically when the "Out of Memory" message appears, it is because
MATLAB asked the operating system for a segment of memory larger than
that which is currently available. Use of the above mentioned
techniques, pre-allocation, CLEAR, PACK, SAVE, and LOAD to help
optimize the available memory. If the "Out of Memory" message still
appears consistently:

o Increase the size of the swap file.

o Make sure that there are no external constraints on the memory
accessible to MATLAB - on Unix systems use the limit command to
check

o Add more memory to the system.

o Reduce the size of the data you are using
--
William York
wil...@mathworks.com

0 new messages