MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

11 Improving Performance and Memory Usage c = rand(2.1e6,1); The following statements can use approximately 32.4 MB of RAM. This is because MATLAB may not be able to reuse the space previously occupied by two 1MB arrays when allocating space for a 2.1 MB array: a = rand(1e6,1); b = rand(1e6,1); clear c = rand(2.1e6,1); The simplest way to prevent overallocation of memory is to allocate the largest vectors first. These statements use only about 16.4 MB of RAM: c = rand(2.1e6,1); clear a = rand(1e6,1); b = rand(1e6,1); Working with Large Amounts of Data 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. 11-20

Resolving “Out of Memory” Errors Resolving “Out of Memory” Errors MATLAB generates an Out of Memory message whenever it requests a segment of memory from the operating system that is larger than what is currently available. This section covers the following topics providing suggestions on how to resolve such errors: • “General Suggestions for Reclaiming Memory” on page 11-21 • “Compressing Data in Memory” on page 11-22 • “Increasing System Swap Space” on page 11-22 • “Freeing Up System Resources on Windows Systems” on page 11-23 • “Reloading Variables on UNIX Systems” on page 11-23 General Suggestions for Reclaiming Memory When you see the Out of Memory message, use any of the techniques discussed under “Using Memory Efficiently” on page 11-12 to help optimize the available memory. If the Out of Memory message still appears, you can try any of the following: • Compress data to reduce memory fragmentation • If possible, break large matrices into several smaller matrices so that less memory is used at any one time. • If possible, reduce the size of your data. • Make sure that there are no external constraints on the memory accessible to MATLAB. (On UNIX systems, use the limit command to check). • Increase the size of the swap file. We recommend that your machine be configured with twice as much swap space as you have RAM. See “Increasing System Swap Space” on page 11-22, below. • Add more memory to the system. On machines running Windows 2000 Advanced Server or Windows 2000 Datacenter Server, the amount of virtual memory space reserved by the operating system can be reduced by using the /3GB switch in the boot.ini file. More documentation on this option can be found at the following URL: 11-21

11 Improving Performance and Memory Usage<br />

c = rand(2.1e6,1);<br />

The following statements can use approximately 32.4 MB of RAM. This is<br />

because <strong>MATLAB</strong> may not be able to reuse the space previously occupied by<br />

two 1MB arrays when allocating space for a 2.1 MB array:<br />

a = rand(1e6,1);<br />

b = rand(1e6,1);<br />

clear<br />

c = rand(2.1e6,1);<br />

The simplest way to prevent overallocation of memory is to allocate the largest<br />

vectors first. These statements use only about 16.4 MB of RAM:<br />

c = rand(2.1e6,1);<br />

clear<br />

a = rand(1e6,1);<br />

b = rand(1e6,1);<br />

Working with Large Amounts of Data<br />

If your program generates very large amounts of data, consider writing the<br />

data to disk periodically. After saving that portion of the data, use the clear<br />

function to remove the variable from memory and continue with the data<br />

generation.<br />

11-20

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!