フィルターのクリア

Out of memory when assigning values to existing arrays?

5 ビュー (過去 30 日間)
Christian
Christian 2012 年 8 月 23 日
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 Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by