Matlab memory optimization regarding output arguments

1 view (last 30 days)
Suppose we have the following code in functie.m file:
"
function functie()
x = [9];
x = functieLocala(x);
disp(x);
end
function y = functieLocala(x)
if length(x) > 10
y = [x 1];
else
y = x;
end
end
"
The question is will Matlab make the output argument of functieLocala y be a copy of the input argument x, or y will share the data of x? On one hand, function called functie tells that x doesn't need to be preserved, but I do not know how functieLocala behaves in terms of copying or sharing the value(s) of the input argument x to the output argument y.

Accepted Answer

lazymatlab
lazymatlab on 14 Dec 2023
Edited: lazymatlab on 14 Dec 2023
It depends.
If the else block runs (y = x) in functieLocala, y shares x because it is just another name of same matrix. If length(x) > 10, then y is not same as x. So the values of x are copied to y and they does not share anymore.
You can test it using memory and making a sufficient large array.
clear
memory
x = functie();
memory
function x = functie()
x = rand(10000);
x(1,1) = 0; % change here and see what happens
memory
x = functieLocala(x);
memory
end
function y = functieLocala(x)
if x(1,1) ~= 0
y = [x rand(10000, 1)];
else
y = x;
end
memory
end
  3 Comments
lazymatlab
lazymatlab on 15 Dec 2023
It says,
"When you assign an array to a second variable (for instance, when you execute B = A), MATLAB does not allocate new memory right away. Instead, it creates a copy of the array reference. As long as you do not modify the contents of the memory block being referenced by A and B, there is no need to store more than one copy of data. However, if you modify any elements of the memory block using either A or B, MATLAB allocates new memory, copies the data into it, and then modifies the created copy."
Bruno Luong
Bruno Luong on 15 Dec 2023
Edited: Bruno Luong on 15 Dec 2023
No worry, the copy on write I know for decades.
But my point is MATLAB can even go further in the optimization and might do NOT do copy-on-write if the function prototype is
x = functieLocala(x)
for both if branch, and the local x is on functie does not share with anything. This is call inplace assigment and the Loren blog (article 2007) recommends to do that. She uses Windows memory to show the behavior NOT MATLAB memory since it is not observable by this MATLAB command.
Recent MATLAB does even further memory optimization from few of the tests, the behavior is not very clear to me, but it looks like it can keep track of variable read and write of user intention, and do something smart on that.

Sign in to comment.

More Answers (1)

Bruno Luong
Bruno Luong on 14 Dec 2023
You better have your funtion codded like this
function functie()
x = 9; % Remove the braket
x = functieLocala(x);
disp(x);
end
function x = functieLocala(x) % make inplace otput when it can
if size(x,2) > 10 % use size rather than length
x = [x 1];
% remove the else branch
end
end

Categories

Find more on Performance and Memory in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!