Profiling Matlab memory usage

Anyone who has had experience with real-life applications knows that Memory usage can have a significant impact on the application’s usability, in aspects such as performance, interactivity, and even (on some lousy memory-management Operating Systems) crashes/hangs.

In Matlab releases of the past few years, this has been addressed by expanding the information reported by the built-in memory function. In addition, an undocumented feature was added to the Matlab Profiler that enables monitoring memory usage.

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

Profile report with memory & JIT info

Profile report with memory & JIT info

In Matlab release R2008a (but not on newer releases) we could also use a nifty parameter of the undocumented feature function:

>> feature mtic; a=ones(100); feature mtoc
ans = 
      TotalAllocated: 84216
          TotalFreed: 2584
    LargestAllocated: 80000
           NumAllocs: 56
            NumFrees: 43
                Peak: 81640

As can easily be seen in this example, allocating 1002 doubles requires 80000 bytes of allocation, plus some 4KB others that were allocated (and 2KB freed) within the function ones. Running the same code line again gives a very similar result, but now there are 80000 more bytes freed when the matrix a is overwritten:

>> feature mtic; a=ones(100); feature mtoc
ans = 
      TotalAllocated: 84120
          TotalFreed: 82760
    LargestAllocated: 80000
           NumAllocs: 54
            NumFrees: 49
                Peak: 81328

This is pretty informative and very handy for debugging memory bottlenecks. Unfortunately, starting in R2008b, features mtic and mtoc are no longer supported “under the current memory manager. Sometime around 2010 the mtic and mtoc features were completely removed. Users of R2008b and newer releases therefore need to use the internal structs returned by the memory function, and/or use the profiler’s memory-monitoring feature. If you ask me, using mtic/mtoc was much simpler and easier. I for one miss these features.

In a related matter, if we wish to monitor Java’s memory used within Matlab, we are in a bind, because there are no built-in tools to help us. there are several JVM switches that can be turned on in the java.opts file: -Xrunhprof[:help]|[:option=value,…], -Xprof, -Xrunprof, -XX:+PrintClassHistogram and so on. There are several memory-monitoring (so-called “heap-walking”) tools: the standard JDK jconsole, jmap, jhat and jvisualvm (with its useful plugins) provide good basic coverage. MathWorks has posted a tutorial on using jconsole with Matlab. There are a number of other third-party tools such as JMP (for JVMs 1.5 and earlier) or TIJMP (for JVM 1.6). Within Matlab, we can use utilities such as Classmexer to estimate a particular object’s size (both shallow and deep referencing), or use java.lang.Runtime.getRuntime()‘s methods (maxMemory(), freeMemory() and totalMemory()) to monitor overall Java memory (sample usage).

We can monitor the Java memory (which is part of the overall Matlab process memory) usage using Java’s built-in Runtime class:

>> r=java.lang.Runtime.getRuntime
r =
>> r.freeMemory
ans =
>> r.totalMemory
ans =
>> usedMemory = r.totalMemory - r.freeMemory;

Specifically in R2011b (but in no other release), we can also use a built-in Java memory monitor. Unfortunately, this simple and yet useful memory monitor was removed in R2012a (or maybe it was just moved to another package and I haven’t found out where… yet…):


Matlab R2011b's Java memory monitor

Matlab R2011b's Java memory monitor

As I have already noted quite often, using undocumented Matlab features and functions carries the risk that they will not be supported in some future Matlab release. Today’s article is a case in point.

Categories: High risk of breaking in future versions, Memory, Stock Matlab function, Undocumented function

Tags: , , , ,

Bookmark and SharePrint Print

8 Responses to Profiling Matlab memory usage

  1. julien says:

    Have you ever used jtimp to profile java memory inside matlab?
    I added “-agentlib:tijmp” in the java.opts file, added the tijmp.dll directory path in librarypath.txt file but matlab does not start any more (OS is windows XP)
    Thanks !

    • @Julien – I’m afraid I don’t have a specific experience with tijmp. Your problem may be due to a missing jdwp.dll library file, which is required by the Java debugger but missing from Matlab’s JRE distribution. See MathWorks technical solution 1-OVU1L. Possibly some other DLL is missing. Other reported problems with the agentlib that cause Matlab to fail to start were reported here. I briefly discuss this in section 1.6 of my book, by the way.

      If you do learn anything new, please post a followup comment here, for the benefit of others.

  2. Pingback: Internal Matlab memory optimizations | Undocumented Matlab

  3. Pingback: File deletion memory leaks, performance | Undocumented Matlab

  4. Pingback: Undocumented Profiler options part 2 | Undocumented Matlab

  5. Ian Schillebeeckx says:

    What about:

    profiler -memory on


  6. Pingback: Assessing Java object size in Matlab | Undocumented Matlab

Leave a Reply to Ian Schillebeeckx Cancel reply

Your email address will not be published. Required fields are marked *