Out of memory when assigning values to existing arrays?

5 views (last 30 days)
Christian on 23 Aug 2012
while dealing with several large arrays I'm facing some strange out of memory problems, which I don't understand. The problems occurred when I was trying to find a workaround for operations on large arrays...
1) It makes a difference if you create a new array (say by zeros(1e10,1), OOM Error) or if you copy an existing array of the same size to a new variable (new = old, no OOM Error). I thought this shouldn't make any difference in terms of memory usage?
2) Changing values in arrays causes an OOM Error, e.g. replace the first value in the array x = zeros(1e10,1) by x(1) = 1;. I would have expected that this operation does not influence the memory?
Any comments appreciated
Thanks, Christian

Accepted Answer

Jan on 23 Aug 2012
Edited: Jan on 23 Aug 2012
This is the expected behavior. Matlab uses a copy-on-write strategy:
a = rand(1e9, 1);
b = a; % Shared data
Now the data pointer of b points to the same memory as a. But when any element of b is modified, the data are duplicated:
b(1) = 1; % Data duplication
This strategy is useful when data are provided to functions. Then a shared data copy saves time and memory as long as no values are modified.
  1 Comment
Christian on 23 Aug 2012
Cheers, this makes sense. Any ideas to the 2nd point? In your example the memory of a is allocated. Now change a(1) = 1; This shouldn't change the memory I thought, but it causes an OOM error...

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!