How can I monitor how much memory MATLAB is using?

147 次查看(过去 30 天)
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
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