Out of memory when assigning values to existing arrays?

5 次查看(过去 30 天)
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

采纳的回答

Jan
Jan 2012-8-23
编辑:Jan 2012-8-23
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 个评论
Christian
Christian 2012-8-23
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...

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by