How do I pre-allocate memory when using MATLAB?

582 views (last 30 days)
I have several FOR loops in my code. I have tried using the PACK function, but I continue to get "out of memory" errors.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 21 Jan 2010
If the matrix size is not defined prior to populating it with data through a FOR loop, memory fragmentation problems may happen since MATLAB is not aware of the final matrix size upon the conclusion of the FOR loop. For example, look at the following FOR loop:
for i=1:10
When this FOR loop is executed, MATLAB looks at the i=1, requests enough memory from the operating system to create a 1 x 1 matrix, and creates x(1)=1. When i=2, MATLAB requests more memory so a 1 x 2 matrix can be stored. If this additional memory is in the same continuous memory strip as when x(1)=1, MATLAB will simply add the additional number in the same memory strip. If the original memory strip is only big enough for a 1x1 matrix, MATLAB moves the x(1)=1 and places it into a memory spot that is large enough for the 1x2 matrix. Since the matrix is now 1x2, the original memory slot is useless to MATLAB for any matrix larger than 1x1. This memory is now fragmented, and this would cause significant problems with large FOR loops.
In order to work around this issue, you should pre-allocate memory by creating an initial matrix of zeros with the final size of the matrix being populated in the FOR loop. For example, if you create a large matrix by typing a = zeros(1000), MATLAB will reserve enough contiguous space in memory for the matrix 'a' with size 1000x1000. This way, instead of looking for a new block of contiguous free space in memory every time 'a' grows larger than the block that holds it, MATLAB will now only change the values in the pre-allocated memory space for the matrix 'a'. Following is an example on how to pre-allocate memory before entering a FOR loop:
for i=1:30,
for j=1:30
The above creates x which is a 30x30 matrix. This gives MATLAB a memory block large enough so it doesn't have to keep asking for fragmented memory. This method reduces the chance of receiving "Out of Memory" errors due to fragmentation. It also improves the performance of the program, as shown in the following code:
Running the following code where memory is NOT pre-allocated:
for i=1:1000,
for j=1:1000,
Elapsed time is 12.175349 seconds.
On the other hand, pre-allocating the memory ahead of time
for i=1:1000,
for j=1:1000,
Elapsed time is 1.761482 seconds.
For pre-allocating memory for a cell array, you may use the following command:
c = cell(m, n, p,...)
It creates an m-by-n-by-p-... cell array of empty matrices. Arguments m, n, p,... must be scalars.
It should be noted that preallocating memory does not make sense if you do not know the eventual size of the matrix you wish to create. This is because one of two cases are likely to occur. Either the preallocated memory will either be too large, resulting in wasted memory; or the allotted memory will be too small for the matrix you are trying to create, resulting in the need to allocate more memory and copy matrix elements to the new space. The latter case will cause you to be just as vulnerable to the memory fragmentation problems you are trying to avoid by preallocating memory.
wedad majeed
wedad majeed on 13 Jun 2019
Dear sir
I worked on using Ritz method which uses a series, and when i used no. of element say 23 term it takes one hour to give me the eigenvalues, how can I minimize this time?
thank u for your help and time
Dr.Widad Ibraheem Majeed

Sign in to comment.

More Answers (3)

David Manuel Buitrago Montañez
Edited: David Manuel Buitrago Montañez on 19 May 2016
Hello, i'm following your advice, but the warning remains
y = zeros(d,2); % preallocating
for i = 1:nargs
y = varargin{i};
if length(y)<d
% llenando los vectores de ceros hasta tf
c = zeros(d-length(y),2); % dos columnas de d ceros
y =[y;c]; % the warning remains
Y{i} = y; % guarda las matrices en celdas
Can anybody explain why? Thanks
Opps!, I almost forget to mention that there are two warnings in this script. the first one is: "the variable "y" appears to be preallocated, but preallocation is not recommended here" the second one is: "The variable "y" appears to change size on every loop iteration. Consider preallocating for speed" And I'm like: [WHAAAAAT?]
Walter Roberson
Walter Roberson on 27 Sep 2016
The code will give an answer if you wait long enough. However you are asking it to investigate 60742943901 combinations of values, which is over 2^35 combinations, so it will take rather some time.
Your problem is not with pre-allocation, your problem is that you need to vectorize your code.

Sign in to comment.

Ahmed Diab
Ahmed Diab on 3 Apr 2019
How do I preallocate P and Location
case 1 %Point
N = input('Input number of point loads. ');
for Count = 1:N
P(Count) = input('Input Magitude of force on point of load. ');
Location(Count) = input('Input location of point load. ');

uma vellaisamy
uma vellaisamy on 29 Apr 2021
error showing as the variable p appears to change size on every loop iteration (within script ). consider prellocatting speed
how to create it

MathWorks Support


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!