How to find the amount of data generated/memory requirment for a program?

4 views (last 30 days)
  • How to know the computational memory requirement of a program?
  • Also I would like to plot between computational time(run and time) and computational memory during a single run of a program.
  • How to obtain a least computational time and least computational memory requirement for a given computational capacity?
Thanks in advance!

Accepted Answer

John D'Errico
John D'Errico on 28 Aug 2016
Edited: John D'Errico on 28 Aug 2016
Sorry. It is not possible to do these things in general.
MATLAB uses dynamically generated variables and arrays. So the memory required can easily vary according to the inputs of your function. And since temporary variables will be created by functions that are called, they may be arbitrarily large.
Suppose you have a function that calls a function. Down in the depths of one of these sub-functions, it sometimes chooses to call the function perms(1:1000), based perhaps on something unknowable. Or worse, suppose you have code that looks like this:
n = input('what is n?')
str = ['A = perms(1:',num2str(n),');'];
eval(str)
Yes, I know, obscene code. But if someone types in 1000 to that question, the result will be something that will take more space than any computer would have in this universe, even if every single elementary particle in the universe was a byte of RAM. Worse, MATLAB cannot know in advance this would happen. So it cannot look at that code without evaluating it to know ho much memory will be required.

More Answers (1)

Thorsten
Thorsten on 29 Aug 2016
Edited: Thorsten on 29 Aug 2016
You can use memorywhos
function memory_in_use = memorywhos
%MONITOR_MEMORY_WHOS uses the WHOS command and evaluates inside the BASE
%workspace and sums up the bytes. The output is displayed in MB.
%
% Modified from
% https://de.mathworks.com/matlabcentral/answers/uploaded_files/1861/monitor_memory_whos.m
mem_elements = evalin('base', 'whos');
memory_in_use = sum([mem_elements(:).bytes])/2^20;
if nargout == 0
disp(['Memory in use ', num2str(memory_in_use, '%.6f'), ' MB'])
clear memory_in_use
end
  1 Comment
Steven Lord
Steven Lord on 6 Sep 2016
That's not necessarily accurate, or even close to accurate.
A = eye(1000);
f = @(x) A*x;
whos A f
So A is very large and f is very small.
clear A
whos
fws = functions(f)
fws.workspace{1}
The anonymous function handle f contains a copy of A, but that is not listed in the memory required by the variable f in whos.
"Hiding" data inside an anonymous function is not the only way to demonstrate this behavior; storing data in a private property of an object, putting it in a containers.Map object, storing it in the global workspace, making it a persistent variable inside a function, etc. are other techniques you can use that would make this underestimate how much memory is in use.
On the other hand, the copy-on-write behavior of MATLAB is a technique that could cause this to overestimate how much memory is in use.
A = ones(100);
B = A;
C = A;
whos A B C
This isn't actually using three times the amount of memory A uses, even though that's how it looks to whos.

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!