MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 730 and 731: 9 Classes and Objects Object Preced
- Page 732 and 733: 9 Classes and Objects How MATLAB De
- Page 734 and 735: 9 Classes and Objects Private funct
- Page 736 and 737: 9 Classes and Objects 9-76
- Page 738 and 739: 10 Scheduling Program Execution wit
- Page 740 and 741: 10 Scheduling Program Execution wit
- Page 742 and 743: 10 Scheduling Program Execution wit
- Page 744 and 745: 10 Scheduling Program Execution wit
- Page 746 and 747: 10 Scheduling Program Execution wit
- Page 748 and 749: 10 Scheduling Program Execution wit
- Page 750 and 751: 10 Scheduling Program Execution wit
- Page 752 and 753: 10 Scheduling Program Execution wit
- Page 754 and 755: 10 Scheduling Program Execution wit
- Page 756 and 757: 10 Scheduling Program Execution wit
- Page 758 and 759: 10 Scheduling Program Execution wit
- Page 760 and 761: 10 Scheduling Program Execution wit
- Page 762 and 763: 11 Improving Performance and Memory
- Page 764 and 765: 11 Improving Performance and Memory
- Page 766 and 767: 11 Improving Performance and Memory
- Page 768 and 769: 11 Improving Performance and Memory
- Page 770 and 771: 11 Improving Performance and Memory
- Page 772 and 773: 11 Improving Performance and Memory
- Page 774 and 775: 11 Improving Performance and Memory
- Page 776 and 777: 11 Improving Performance and Memory
- Page 778 and 779: 11 Improving Performance and Memory
- Page 782 and 783: 11 Improving Performance and Memory
- Page 784 and 785: 11 Improving Performance and Memory
- Page 786 and 787: 12 Programming Tips MATLAB Path (p.
- Page 788 and 789: 12 Programming Tips numberOfLines)
- Page 790 and 791: 12 Programming Tips Help This secti
- Page 792 and 793: 12 Programming Tips Topic Name arit
- Page 794 and 795: 12 Programming Tips Youcangethelpon
- Page 796 and 797: 12 Programming Tips Commenting Out
- Page 798 and 799: 12 Programming Tips For M-file func
- Page 800 and 801: 12 Programming Tips Function Argume
- Page 802 and 803: 12 Programming Tips Passing Argumen
- Page 804 and 805: 12 Programming Tips For more inform
- Page 806 and 807: 12 Programming Tips Debugging This
- Page 808 and 809: 12 Programming Tips Setting Breakpo
- Page 810 and 811: 12 Programming Tips Variables This
- Page 812 and 813: 12 Programming Tips AvoidUsingiandj
- Page 814 and 815: 12 Programming Tips Strings This se
- Page 816 and 817: 12 Programming Tips Function regexp
- Page 818 and 819: 12 Programming Tips are useful when
- Page 820 and 821: 12 Programming Tips File Precedence
- Page 822 and 823: 12 Programming Tips Making Nontoolb
- Page 824 and 825: 12 Programming Tips Using switch Ve
- Page 826 and 827: 12 Programming Tips try X = A * B c
- Page 828 and 829: 12 Programming Tips • Load spread
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