How can I monitor how much memory MATLAB is using?

150 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 5 de Sept. de 2018
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 comentario
Kevin Gleason
Kevin Gleason el 27 de Sept. de 2016
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.

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 4 de Mayo de 2016
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 comentario
Walter Roberson
Walter Roberson el 25 de Oct. de 2016
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Performance and Memory en Help Center y File Exchange.

Etiquetas

Productos


Versión

R14SP1

Community Treasure Hunt

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

Start Hunting!

Translated by