Efficient use of memory

3 views (last 30 days)
emar
emar on 8 Jun 2017
Hello,
If i create a cell with 1000 matrices ( size of each matrix 800*1280), clearing each matrix after using it will speed up calculations ?
Example
A=cell(1000,1);
for i=1:1000
A{i}=rand(800,1280);
end
image=A{1};
image2=A{2}; % I will use image and image2 with other functions
A{1}=[];
A{2}=[];
EDIT
The real use of the cell will be like :
A=cell(1000,1);
parfor i=1:1000
A{i}=function_that_creates_image(800,1280); % image with size 800*1280 px
end
for i=1:number_of_images % number_of_images=1000 in this case
image1=A{1};
image2=A{2};
A{1}=[];
A{2}=[];
% image1 and image 2 will be used then in the next lines
%next lines of code
end
I noticed that calculating components of A in a parfor loop is faster than calculating each component for each loop inside the for loop
Thank you in advance
  2 Comments
Adam
Adam on 8 Jun 2017
Edited: Adam on 8 Jun 2017
Firstly you would be better using a 3d numeric array. Or rather, firstly, why do you want 1000 large arrays all at once? Your code only shows you using 2 of them so it's impossible to make meaningful suggestions on how efficient it is having the other 998 in memory. As for clearing the 2 you have used, you should be able to see if the memory goes down in your task manager. I don't know if the memory gets free'd instantly.
As you have shown it though there is no reason to store any more than one matrix at a time.
emar
emar on 8 Jun 2017
I edited the question for more explanations

Sign in to comment.

Accepted Answer

Jan
Jan on 8 Jun 2017
clearing each matrix after using it will speed up calculations ?
It depends on the available RAM. If the RAM is exhausted and the much slower virtaul RAM is used instead, the speed difference can be factor 100 or higher. As soon as memory is released, the chance gorws, that all data match into the RAM.
Note that
image1=A{1};
creates a "shared data copy", such that only about 100 Bytes are used in addition, while the actual data are shared. Then:
A{1}=[];
does not release the memory directly, but this happens after image1 is overwritten or cleared also.
As Adam, I would expect that creating only the data required for the current iteration is faster. This would avoid to store 998 images in the memory without need.
  1 Comment
Philip Borghesani
Philip Borghesani on 8 Jun 2017
In addition if image1 or image2 is modified using indexing after clearing A{1} and A{2} no copy on write is needed because there is no remaining sharing of the data. The in place optimization will also be enabled for image1 and 2.

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!