MATLAB: Out of memory when assigning values to existing arrays

out of memory

Hi,
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

Best Answer

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.