MATLAB: ‘parfor’ Memory Usage Greater Than Expected

handleMATLABmemoryparfor

Just before the execution of a particular loop in my code the memory usage will be ~6GB.
If I use 'for' then the memory usage will increase by ~600MB during execution of a single loop and drop back down.
If I use 'parfor' with just 2 workers the memory usage will exceed my 32GB of RAM and 32GB of swap.
Here is the loop:
parfor n=1:length( loop_Unique_Handle_Object_Array )
result{n} = someFunction( shared_Data,...
loop_Unique_Handle_Object_Array(n) );
end
'shared_Data' is 100MB
Each instance of loop_Unique_Handle_Object_Array is 200MB
I dont understand why the amount of RAM required by 'parfor' is greater than the amount required by 'for' * numWorkers + Overhead. Is this a result of using handle objects?

Best Answer

Is this a result of using handle objects?
Possibly. All data that a handle object points to will be cloned and transmitted to the workers. It's not clear how you would have measured the memory footprint of everything your handle object points to and concluded that it is 200MB. MATLAB doesn't have any direct tools that would let you measure this footprint (whos() will not be reliable, in particular).
An easy test would be to just do a simplified parfor which only broadcasts the handle objects
parfor n=1:length( loop_Unique_Handle_Object_Array )
result{n} = loop_Unique_Handle_Object_Array(n);
end
Related Question