How can I monitor how much memory MATLAB is using?

조회 수: 210 (최근 30일)
MathWorks Support Team
MathWorks Support Team 2009년 6월 27일
댓글: Walter Roberson 2016년 10월 25일
I have a program that is memory intensive and I want to monitor how much memory MATLAB is using so that if it goes above a certain threshold, I can stop the program.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2018년 9월 5일
Unfortunately, there is not a convenient way to monitor memory usage in MATLAB. However, the attached function can help monitor memory usage as a workaround.
The function, 'monitor_memory_whos.m' operates by using the WHOS command and evaluating it within the 'base' workspace. Each variable's memory usage is summed up and converted into megabytes. This function can be run in the background without displaying data to the MATLAB command prompt. However, the memory usage of the workspace is not the only memory used by MATLAB. Typically, the program starts up using approximately 500 MB of memory.
To run the 'monitor_memory_whos.m' function please type the following the MATLAB command prompt:
A = magic(1000);
B = phantom(500);
C = peaks(250);
in_use = monitor_memory_whos
The memory function can also be used to monitor memory usage on Windows systems. For more information on memory management, please refer to the following link:
  댓글 수: 1
Kevin Gleason
Kevin Gleason 2016년 9월 27일
Short Answer: MB, and values in the workspace.
The monitor_memory_whos.m relies on evaluating the "whos" function in the base workspace.
Try executing "whos" in your command window. It displays the bytes of all elements in the workspace. The monitor_memory_whos script add those byte values and divides by 2^20 to display workspace memory in MB.

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

추가 답변 (1개)

Jan
Jan 2016년 5월 4일
Note that "memory usage" is not well defined. When the code let an array grow iteratively, it requests new memory in each iteration:
v = [];
for k = 1:1e6
v(k)= rand;
end
Although the final array uses 8MB only, Matlab requests sum(1:1e6)*8 = 500GB of memory from the OS. Of course Matlab releases the memory, but the OS waits until it finds time to clear the memory and overwrite it with zeros. Therefore even this cute loop can exhaust the RAM. But do you consider the released and not yet cleared RAM as "occupied by Matlab" or not?
If you open a lot of files simultaneously in Matlab, the memory for caching reserved by the OS can grow to a remarkable size. This is caused by Matlab, but the memory does not belong to Matlab in the taskmanager. Matlab can store data on the graphic card also.
If several threads, e.g. in parfor, access data in the same cache-line (usually a 64 byte size block of memory), the total performance can degrade drastically. This could be considered as "memory intensive task" also, although it concerns a tiny memory block only.
In consequence "memory usage" is as vague as "processor time" in modern operating systems.
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 10월 25일
MATLAB keeps a "small block" memory pool and will clean it out as needed. I do not know if it will re-use the same location over and over or if it uses a more round-robin strategy.

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

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

제품


릴리스

R14SP1

Community Treasure Hunt

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

Start Hunting!

Translated by