In Part 1 and Part 2 of this blog series, I discussed optimizations that you can perform on MATLAB scripts to enhance both their processing performance and memory usage. Such optimizations become increasingly important when implementing scripts for new algorithms as the amount of data they process and their complexity tend to grow. In a conception-to-development process, every bit of time saved in the implementation-test-validation-correction work loop can translate into big overall gains in time-to-market and, as we all know, in the end, time is money.

While looking for efficient ways to trace the memory usage of a script throughout its execution in order to ease the task of identifying which unused memory buffers can be “cleared”, I stumbled upon a great, undocumented feature of the MATLAB Profiler: by typing the command profile –memory on, new columns appear in the profiler tool that display memory-related information about your script.

Figure 1: Normal profiler window

Figure 1 Normal profiler window

Figure 2 Profiler with the -memory option "on"

Figure 2 Profiler with the -memory option “on”

This tool displays information about allocated and freed memory within your script as well as its memory usage. The amount of memory allocated and freed within the script appears to yield good results, though I’m pretty sure the Peak Memory column is incorrect (the correct result would be the peak memory usage during the execution of the function, which should be higher than 64 MB in the case of the “MyScript” example used above). That being said, it is an undocumented feature it’s probably incomplete or not tested so such behaviour can be expected. Still, the rest of the information is very useful.

If you click on a function, you’ll get a more in-depth report. At the bottom of this report, you can select different features you want to highlight, such as the time spent on a line, or lines where memory was allocated.

Figure 3 Feature to highlight menu

Figure 3 Feature to highlight menu

By selecting the “allocated memory” feature, you can see every line of your script where memory is allocated, so it’s easy to find specific lines when working with big files.

Figure 4 Highlight lines where memory is allocated

Figure 4 Highlight lines where memory is allocated

An interesting and useful feature when fixing memory allocation problems is when you highlight the lines shown by “allocated memory”. Dark green is used to highlight the line allocating the most memory and the color becomes lighter as the amount of memory allocated gets smaller. This makes it easy to find the lines of your script that use the most memory. The same goes when you choose to highlight the lines by “time” – you can easily identify the time-consuming lines in order to try and optimize them.

Figure 5 Shading variation depending of the amount of memory allocated

Figure 5 Shading variation depending of the amount of memory allocated

This option (-memory on) MATLAB Profiler command is not perfect. That being said, it is an undocumented tool so flukes are to be expected and it still gives interesting information about your scripts that can assist in your optimizations.

Reference:

http://undocumentedmatlab.com/blog/undocumented-profiler-options/