MATLAB Answers

0

How can I monitor how much memory MATLAB is using?

Asked by MathWorks Support Team on 27 Jun 2009
Latest activity Edited by Mark T
on 30 May 2018
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.

Tags

Products


Release

R14SP3

4 Answers

Answer by MathWorks Support Team on 5 Sep 2018
 Accepted Answer

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:

  2 Comments

The monitor_memory_whos.m gives memory usage in units of? And what memory elements does it take into account?
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.

Sign in to comment.


Answer by Jan
on 4 May 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.

  2 Comments

Okay, I can understand the logic here, but is MATLAB really not clearing the size-1 array after creating the size-2 array? That's nuts! Is there any reason for Matlab to hold onto that memory when those earlier-iteration arrays each become immediately inaccessible on the next iteration? I knew that growing an array was a bad idea, but apparently it's an even worse idea than I thought.
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.

Sign in to comment.


Answer by Mark T
on 18 Jul 2017
Edited by Mark T
on 30 May 2018

Hi, here is one option for Windows that seems to work:
function memory_kb=get_process_memory1(process)
if ~exist('process','var')
process='Matlab.exe';
end
command=sprintf('tasklist /nh /fi "imagename eq %s"',process);
[status, result] = dos(command);
result=textscan(result,'%s');
m=result{1}{5};
% Deal with the separators - sigh...
% German OS
local_dec_sep=',';
local_1000_sep='.';
i_1000=find(m==local_1000_sep);
i_dec=find(m==local_dec_sep);
if ~isempty(i_dec),
m_dec=m(i_dec+1:end);
m=m(1:i_dec-1);
else
m_dec='';
end
m(i_1000)='';
m=[m '.' m_dec];
memory_kb=str2num(m);
You will need to switch the ',' and '.' for English OS. Could easily upgrade to handle multiple processes with the same name. A shame it takes 100 ms on my machine...

  0 Comments

Sign in to comment.


Answer by Qifan
on 4 May 2016

Task Manager, memory usage are shown under the process section.

  0 Comments

Sign in to comment.