Can anyone explain why Matlab gets slower and slower until restart if large cell or struct arrays are allocated and cleared repeatedly, but only on Windows, not Linux?

37 Ansichten (letzte 30 Tage)
I am experiencing a serious performance problem with some matlab code that I have simplified with the attached example. I would appreciate any help in explaining how to avoid or workaround this issue, and also anyone who can run my example and confirm that they see the same result. I am very surprised at how badly this affects performance and how simple it is to reproduce (How many people are suffering from this unknowingly?). Here are some properties of this issue I've noticed:
  1. execution time increases fairly linearly with each reinitialization of a large cell array or struct array.
  2. execution time remains slow until Matlab is restarted.
  3. execution time of other unrelated code with different variables that also meet the criteria is affected until restart.
  4. Only occurs if both the cell or struct array containers are large enough (around 2000 elements or more)
  5. Only occurs if each cell element or struct array element contains a large matrix (around 200 x 200 or more)
  6. Only occurs on Windows (Linux runs the same example code without issues). I am using Windows 7 64 bit (16GB ram)
  7. Occurs on all recent versions of Matlab that I have tried (R2015b through R2017a)
  8. Occurs in a script (See SlowingExecutionIssueSimple.m or the code pasted below)
  9. Occurs in a function, but with slightly different conditions. Does not occur if the variable is persistent for certain initialization methods. Occurs if the variable is not persistent regardless of initialization method. See SlowingExecutionIssue.m for easy testing of all these cases.
  10. After many iterations of the outer loop, the growth in execution time eventually seems to level off, but at a completely unusable level of performance compared to the first iteration.
  11. Occurs even with fully preallocated arrays (in some cases, it occurs especially if fully preallocated - see struct array example in SlowingExecutionIssue.m for case where incomplete preallocation avoids this problem)
  12. "clear all" and "clear classes" have no effect.
Thanks in advance for any help with this. So far I have only been able to work around it in my application code by making variables persistent inside a function and avoiding reinitialization (i.e. reinitializing only one element at a time inside the innermost loop). It is not always possible to work around it and requires constant use of the memory that could otherwise be freed. Is this a bug in how Matlab manages memory?, or an unavoidable side effect of windows memory management? If it doesn't get fixed soon, I may have to switch permanently to Linux to avoid this issue.
iter = 10000;
for i = 1:10
tic;
a0 = ones(221,241);
a = a0;
% c=cell(iter,1);%Cell array example. The problem only occurs if c is reinitialized each time
f = repmat(struct('a',a),1,iter);%Struct array example. Struct array with all elements populated with "a"
for j=1:iter
%a has to change each iteration or the issue doesn't show up
a=a+1;%Change the variable
a=a-1;%Change the variable back to original value
% c{j}=a;%This causes serious slowdown with each run
f(j).a = a;%This causes serious slowdown with each run
% c{j}=a0;%This runs fast even though a0 is identical in size, type, and value to a
% f(j).a = a0;%This runs fast
end
toc
end
Here is the output I get from this script:
>> SlowingExecutionIssueSimple
Elapsed time is 1.227035 seconds.
Elapsed time is 1.336831 seconds.
Elapsed time is 1.349026 seconds.
Elapsed time is 1.426942 seconds.
Elapsed time is 1.568531 seconds.
Elapsed time is 1.779247 seconds.
Elapsed time is 2.051785 seconds.
Elapsed time is 2.412232 seconds.
Elapsed time is 2.857880 seconds.
Elapsed time is 3.500180 seconds.
>> clear all
>> clear classes
>> SlowingExecutionIssueSimple
Elapsed time is 2.254901 seconds.
Elapsed time is 4.609098 seconds.
Elapsed time is 5.811552 seconds.
Elapsed time is 6.322061 seconds.
Elapsed time is 6.974427 seconds.
Elapsed time is 7.242437 seconds.
Elapsed time is 7.527272 seconds.
Elapsed time is 7.704636 seconds.
Elapsed time is 8.079658 seconds.
Elapsed time is 8.315676 seconds.
>>

Antworten (1)

Jan
Jan am 26 Mär. 2017
Bearbeitet: Jan am 26 Mär. 2017
With 20 iteration I get (Matlab R2016b/64, Win7, Core2Duo, ):
Elapsed time is 12.796962 seconds.
Elapsed time is 5.915845 seconds.
Elapsed time is 4.910906 seconds.
Elapsed time is 4.819912 seconds.
Elapsed time is 5.078799 seconds.
Elapsed time is 5.410653 seconds.
Elapsed time is 5.353264 seconds.
Elapsed time is 5.862806 seconds.
Elapsed time is 6.867665 seconds.
Elapsed time is 7.050129 seconds.
Elapsed time is 7.989765 seconds.
Elapsed time is 9.024225 seconds.
Elapsed time is 10.223250 seconds.
Elapsed time is 11.062545 seconds.
Elapsed time is 11.918006 seconds.
Elapsed time is 12.276263 seconds.
Elapsed time is 12.650565 seconds.
Elapsed time is 12.974336 seconds.
Elapsed time is 13.291335 seconds.
Elapsed time is 13.585811 seconds.
After
clear all
pack
th results are:
Elapsed time is 7.361198 seconds.
Elapsed time is 14.111978 seconds.
Elapsed time is 14.360832 seconds.
Elapsed time is 14.552819 seconds.
... stable timings
This looks like a problem of the memory manager of the OS.
[EDITED] 10 iteration in R2009a:
Elapsed time is 3.651902 seconds.
Elapsed time is 4.155962 seconds.
Elapsed time is 4.235177 seconds.
Elapsed time is 4.178624 seconds.
Elapsed time is 4.218829 seconds.
Elapsed time is 4.560025 seconds.
Elapsed time is 4.319851 seconds.
Elapsed time is 4.316553 seconds.
Elapsed time is 4.300376 seconds.
Elapsed time is 4.294655 seconds.
Well, I admint: Not a problem of the OS, but of Matlab's memory manager.
  10 Kommentare
Philip Borghesani
Philip Borghesani am 2 Apr. 2017
Bearbeitet: Philip Borghesani am 2 Apr. 2017
If you search around for MATLAB hints and tricks you can find details on most of them.
Basic concepts to get you started.
  • MATLAB always passes everything by reference and does copy on write.
  • When the same variable is assigned by a function or operation and passed into it from a function then modifications can be done in place and no copy on write is needed. Loren's Blog on in place operations
  • The trick with t is because the in-place optimization does not currently work for a cell in a cell array. Clearing the cell is needed to release the second reference to the array that was "moved" to t.
  • The multiplication by zero is to allow t to be passed into the scalar operation allowing its buffer to be reused. Actual multiplication is so fast compared to memory access and allocation that its cost is irrelevant.
James Tursa
James Tursa am 12 Mai 2017
"... MATLAB always passes everything by reference and does copy on write. ..."
Almost always. There are some circumstances (which escape me at the moment) where MATLAB will pass a deep copy of scalar arguments.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by