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

조회 수: 4 (최근 30일)
  • 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!

채택된 답변

John D'Errico
John D'Errico 2016년 8월 28일
편집: John D'Errico 2016년 8월 28일
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.

추가 답변 (1개)

Thorsten
Thorsten 2016년 8월 29일
편집: Thorsten 2016년 8월 29일
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
Steven Lord
Steven Lord 2016년 9월 6일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by