필터 지우기
필터 지우기

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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by