# How do I pre-allocate memory when using MATLAB?

183 visualizaciones (últimos 30 días)
MathWorks Support Team el 21 de En. de 2010
Editada: Stephen23 el 13 de Mzo. de 2019
I have several FOR loops in my code. I have tried using the PACK function, but I continue to get "out of memory" errors.

MathWorks Support Team el 21 de En. de 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
x(i)=i;
end
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:
x=zeros(30);
for i=1:30,
for j=1:30
x(i,j)=i+j;
end
end
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:
tic;
for i=1:1000,
for j=1:1000,
x(i,j)=i+j;
end
end
toc
returns:
Elapsed time is 12.175349 seconds.
On the other hand, pre-allocating the memory ahead of time
tic;
x=zeros(1000);
for i=1:1000,
for j=1:1000,
x(i,j)=i+j;
end
end
toc
returns:
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.
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Stephen23 el 13 de Mzo. de 2019
Editada: Stephen23 el 13 de Mzo. de 2019
"Is there any way to allocate only an array of integers..."
Simply read the zeros documentation and specify the typename option, e.g.:
zeros(...,'uint8')
"...or even an array of floats"
Both double and single are binary floating point classes which you can specify using typename.

Iniciar sesión para comentar.

### Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by