MATLAB: How to monitor how much memory MATLAB is using

dumpmemMATLAB

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.

Best 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: