Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Undocumented profiler options

April 2, 2009 23 Comments

The Matlab profiler is a powerful tool for debugging performance-related issues in Matlab applications. However, it also has some undocumented options that facilitate other forms of debugging, namely memory bottlenecks and JIT (Just-In-Time compilation) problems.
To turn on memory stats in the profile report, run this (only once is necessary – will be remembered for future profiling runs):

profile -memory on;
profile('-memory','on');  % an alternative

To turn on JIT information, run this (again, only once is necessary, prior to profile report):

setpref('profiler','showJitLines',1);

You will then see additional JIT and memory (allocated, freed and peak) information displayed in the profile report, as well as the options to sort by allocated, freed and peak memory:


Profile report with memory & JIT infoProfile report with memory & JIT info
Profile report with memory & JIT info
Profile report with memory & JIT info

For those interested, the references to these two options appear within the code of profview.m (line 1199 on R2007b), for the JIT option:

showJitLines = getpref('profiler','showJitLines',false);

…and profile.m (lines 163-165 on R2007b), for the memory option:

if memory ~= -1
    callstats('memory', memory);
end

Note that there appears to be two undocumented additional memory-related options in profile.m (lines 311-312):

options = {'detail', 'timer', 'history', 'nohistory', 'historysize', ...
           'timestamp', 'memory', 'callmemory', 'nomemory' };

However, ‘-nomemory’ appears to simply turn the memory stats collection off, and ‘-callmemory’ is not recognized because of a bug in line 349, which looks for ‘callnomemory’…:

    case 'callnomemory'   % should be 'callmemory'
           memory = 2;

When this bug is fixed, we see that we get only partial memory information, so the ‘-callmemory’ option is really not useful – use ‘-memory’ instead.
Addendum (Jan 31, 2011): JIT information has been removed in Matlab 7.12 (R2011a). I assume that this was done so that programmers will not attempt to depend on JITC functionality in their code (see Michelle Hirsch’s comment below). It’s a good thing that the memory options remain, since these are quite useful in profiling memory-related bottlenecks.

Related posts:

  1. Undocumented Profiler options part 3 – An undocumented feature of the Matlab Profiler can report call history timeline - part 3 of series. ...
  2. Undocumented Profiler options part 2 – Several undocumented features of the Matlab Profiler can make it much more useful - part 2 of series. ...
  3. Undocumented Profiler options part 4 – Several undocumented features of the Matlab Profiler can make it much more useful - part 4 of series. ...
  4. Buggy Profiler option – a bug exists in the profiler that prevents usage of its documented CPU timing functionality. ...
  5. Undocumented scatter plot behavior – The scatter plot function has an undocumented behavior when plotting more than 100 points: it returns a single unified patch object handle, rather than a patch handle for each specific point as it returns with 100 or less points....
  6. More undocumented timing features – There are several undocumented ways in Matlab to get CPU and clock data...
callstats Memory Performance Profiler Pure Matlab
Print Print
« Previous
Next »
23 Responses
  1. Michelle Hirsch April 3, 2009 at 04:17 Reply

    Yair –

    Thanks for another interesting post. I’d like to share a couple of comments:

    – We would love feedback on the memory profiling feature. We know there’s a lot of interest in tools to help figure out where and how memory is chewed up in code, but it turns out to be not so obvious as to what information to present and how. I’ll keep an eye out here for comments, or readers can feel free to email me directly. My address is on my MATLAB Central page.

    – I would advise a bit of caution when working with the JIT profiler results. We turned them off after MATLAB 6.5 primarily because the JIT was such a moving target (changing every release) that we really didn’t want to encourage users to code against it. That being said, your readers all know the standard warning that comes with your posts – so have fun poking around!!

  2. Mark Westwood April 8, 2009 at 00:40 Reply

    Yair

    This is rapidly becoming one of my favourite blog sites. keep up the excellent work.

    Mark

  3. Marcel May 20, 2009 at 06:07 Reply

    Profiling the memory sounds really interresting – unfortunately it does NOT work on my computer (Win XP, Maltab R2009a)

    Marcel

  4. tic / toc - undocumented option | Undocumented Matlab May 22, 2009 at 06:12 Reply

    […] I have recently discussed undocumented features in Matlab’s built-in profiler. Another tool often used for ad-hoc performance profiling of Matlab code are the built-in duo functions tic and toc […]

  5. Mark July 12, 2009 at 23:40 Reply

    Good Work

  6. Matthew Davidson March 29, 2010 at 12:13 Reply

    It doesn’t work for me in 2007B, OS X 10.5. It gives me the following error from callstats.m: “Support for memory tracking is not available with the current memory manager.

    This is unfortunate, because lack of memory profiling is one of Matlab’s biggest performance gaps. As someone who works with massive neuroimaging data sets every day, good memory use is more important to me than CPU performance. Shaving an hour or two off an analysis is nice, but using too much memory means my analysis won’t even finish.

    As an ex-professional programmer, I’d seriously considered making and selling something like this as an add-on if I wasn’t in grad school.

    Maybe in that far-off utopia of infinite funding, I can convince my advisors to buy all the necessary copies of later matlab versions. 🙂

    • Yair Altman March 30, 2010 at 10:42 Reply

      @Matthew – please read Scott Hirsch’s comment above. Scott is the Matlab product manager at MathWorks and says that they “would love feedback on the memory profiling feature”. Perhaps you could contact him via the attached link and explain your use-case. There might be a workaround for your Matlab version/platform. Even if not, it might benefit other users who have similar issues. If you learn anything new, please do post it here.

  7. Patrick Mineault October 27, 2010 at 10:46 Reply

    Works great over here, although sometimes gives nonsensical values for memory consumption (negative amounts or terabytes, etc.). Linux 64-bit R2009b.

  8. DA February 10, 2011 at 04:07 Reply

    Memory profiling is great, but it does seem to add a lot of overhead, potentially slowing the profiler by many orders of magnitude.

  9. Chris November 3, 2011 at 15:16 Reply

    The -memory option is a great idea. But as @DA states above it can really slow the profiler down. I had to turn it off, with the -nomemory option, to get reasonable performance.

  10. Dave November 7, 2011 at 21:12 Reply

    Does this work correctly on Linux?

    I got a nonsensical memory value for a mex function (10^16 kB). It is using its own memory allocation internally, though…

  11. Matlab-Java memory leaks, performance | Undocumented Matlab January 19, 2012 at 17:56 Reply

    […] Matlab Profiler’s undocumented memory profiling option helped me quite a bit, as well as lots of intuition and trial-and-error. Detecting memory leak is never easy […]

  12. Profiling Matlab memory usage | Undocumented Matlab February 29, 2012 at 17:13 Reply

    […] the Matlab Profiler enables monitoring memory usage […]

  13. Undocumented Profiler options part 2 | Undocumented Matlab September 19, 2012 at 11:03 Reply

    […] Three years ago I published an article on undocumented profiler options. Since then, I have posted several articles about performance […]

  14. Undocumented Profiler options part 3 | Undocumented Matlab September 26, 2012 at 11:01 Reply

    […] Past articles explained how to profile memory, improve timing resolution and fix a font issue […]

  15. Undocumented Profiler options part 4 | Undocumented Matlab October 3, 2012 at 11:02 Reply

    […] Past articles explained how to profile memory, improve timing resolution and fix a font issue, and display the call history. […]

  16. » 2D and 3D Perlin Noise in MATLAB semifluid.com December 5, 2012 at 06:45 Reply

    […] However interp3 is VERY memory intensive. Here are example MATLAB profiler outputs illustrating the peak memory used by MATLAB (note that I used the undocumented MATLAB profiler memory option “profile -memory on;”, described here): […]

  17. Andrew February 21, 2013 at 10:52 Reply

    Hi Yair,
    Can you explain a little more how you used the profiler to detect leaks? I’m working on a continuously running software (always open, but runs a set of functions/methods every minute or so) that seems to creep up in memory over time. I ran the profiler while executing my code and am trying to understand what I’m looking at. I see allocated, freed, self, and peak memory, but don’t know how that translates to memory growth over time. Any help would be appreciated!

    • Yair Altman February 21, 2013 at 13:27 Reply

      @Andrew – it should be relatively easy to see where allocated>freed: when there are no leaks, these two should have exactly the same value.

      You could also use non-Matlab tools (e.g., Process Explorer for memory leaks or GDIView for GDI handle leaks — both of them for Windows).

      Note in advance that hunting down memory leaks in Matlab is very difficult, due to the lack of good monitoring tools (AFAIK).

      If you or anyone else find any leaks, please report them here or directly to me. I know for a fact that MathWorks’ dev team are very interested in such inputs — whatever will be reported to me I will pass on to them for fixing.

      • Andrew February 22, 2013 at 08:21

        Hi Yair,
        That’s what I figured, but I thought I would ask for clarification anyway. Thanks so much for your quick response and wealth of knowledge! It seems a bit strange. I can run the same set of functions with the same inputs, but the profiler comes back with different results. And I’ll need to determine if it’s my code or Matlab itself causing the memory growth… Well, I will certainly let you know what I can come up with.
        Thanks again!
        ~Andrew

  18. 2D and 3D Perlin Noise in MATLAB | semifluid.com March 23, 2015 at 03:12 Reply

    […] However interp3 is VERY memory intensive. Here are example MATLAB profiler outputs illustrating the peak memory used by MATLAB (note that I used the undocumented MATLAB profiler memory option “profile -memory on;”, described here): […]

  19. jabbar September 1, 2015 at 05:49 Reply

    Hi dear Yair
    i want to know differences between peak memory, self memory, allocated memory and freed memory while we are using Memory monitoring using profiler.
    i google it but it seems no one knows about the issue!
    thanks in advance.

    • Yair Altman September 3, 2015 at 07:38 Reply

      @Jabbar – I believe that these are an approximate description of the various reported values:

      * Peak memory is the maximal amount of allocated memory during the execution of the profiled line/function.
      * Allocated memory is the total amount of memory allocated during the execution. Note that it is a cumulative amount, meaning that if you have an allocation of 1KB in a loop that runs 50 times, you’ll see a 50KB value.
      * Freed memory is the total amount of memory de-allocated during the execution. This too is a cumulative amount.
      * Self memory is the memory used by the profiled line/function, which is not deallocated (and therefore not included in the “Freed memory” value).

      Note that these are speculations: The exact definition of what is reported under which category is internal to Matlab and is currently unknown.

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
ActiveX (6) AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
  • Nicholas (4 days 1 hour ago): Hi Yair, Thanks for the reply. I am on Windows 10. I also forgot to mention that this all works wonderfully out of the editor. It only fails once compiled. So, yes, I have tried a...
  • Nicholas (4 days 1 hour ago): Hi Yair, Thanks for the reply. I am on Windows 10. I also forgot to mention that this all works wonderfully out of the editor. It only fails once compiled. So, yes, I have tried a...
  • Yair Altman (4 days 8 hours ago): Nicholas – yes, I used it in a compiled Windows app using R2022b (no update). You didn’t specify the Matlab code location that threw the error so I can’t help...
  • Nicholas (5 days 5 hours ago): Hi Yair, Have you attempted your displayWebPage utility (or the LightweightHelpPanel in general) within a compiled application? It appears to fail in apps derived from both R2022b...
  • João Neves (8 days 9 hours ago): I am on matlab 2021a, this still works: url = struct(struct(struct(struct(hF ig).Controller).PlatformHost). CEF).URL; but the html document is empty. Is there still a way to do...
  • Yair Altman (11 days 8 hours ago): Perhaps the class() function could assist you. Or maybe just wrap different access methods in a try-catch so that if one method fails you could access the data using another...
  • Jeroen Boschma (11 days 11 hours ago): Never mind, the new UI components have an HTML panel available. Works for me…
  • Alexandre (11 days 12 hours ago): Hi, Is there a way to test if data dictionnatry entry are signal, simulink parameters, variables … I need to access their value, but the access method depends on the data...
  • Nicholas (12 days 2 hours ago): In case anyone is looking for more info on the toolbar: I ran into some problems creating a toolbar with the lightweight panel. Previously, the Browser Panel had an addToolbar...
  • Jeroen Boschma (15 days 9 hours ago): I do not seem to get the scrollbars (horizontal…) working in Matlab 2020b. Snippets of init-code (all based on Yair’s snippets on this site) handles.text_explorer...
  • Yair Altman (43 days 11 hours ago): m_map is a mapping tool, not even created by MathWorks and not part of the basic Matlab system. I have no idea why you think that the customizations to the builtin bar function...
  • chengji chen (43 days 17 hours ago): Hi, I have tried the method, but it didn’t work. I plot figure by m_map toolbox, the xticklabel will add to the yticklabel at the left-down corner, so I want to move down...
  • Yair Altman (51 days 10 hours ago): @Alexander – this is correct. Matlab stopped including sqlite4java in R2021b (it was still included in 21a). You can download the open-source sqlite4java project from...
  • Alexander Eder (57 days 6 hours ago): Unfortunately Matlab stopped shipping sqlite4java starting with R2021(b?)
  • K (63 days 17 hours ago): Is there a way to programmatically manage which figure gets placed where? Let’s say I have 5 figures docked, and I split it into 2 x 1, I want to place 3 specific figures on the...
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top