- Undocumented Matlab - https://undocumentedmatlab.com/blog -

Profiling Matlab memory usage

Posted By Yair Altman On February 29, 2012 | 8 Comments

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 [3] 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 [4]:

>> 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 [5]. 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 [6] file: -Xrunhprof[:help]|[:option=value,…], -Xprof, -Xrunprof, -XX:+PrintClassHistogram and so on [7]. 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 [8] a tutorial on using jconsole with Matlab. There are a number of other third-party tools such as JMP [9] (for JVMs 1.5 and earlier) or TIJMP [10] (for JVM 1.6). Within Matlab, we can use utilities such as Classmexer [11] 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 [12]).

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

>> r=java.lang.Runtime.getRuntime
r =
java.lang.Runtime@5fb3b54
 
>> r.freeMemory
ans =
    86147768
 
>> r.totalMemory
ans =
   268304384
 
>> 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…):

com.mathworks.xwidgets.JavaMemoryMonitor.invoke

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


8 Comments (Open | Close)

8 Comments To "Profiling Matlab memory usage"

#1 Comment By julien On March 1, 2012 @ 2:09 pm

Yair,
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 !

#2 Comment By Yair Altman On March 1, 2012 @ 3:07 pm

@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 [20]. Possibly some other DLL is missing. Other reported problems with the agentlib that cause Matlab to fail to start were reported [21]. 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.

#3 Pingback By Internal Matlab memory optimizations | Undocumented Matlab On May 30, 2012 @ 7:23 am

[…] If we profile our code using any of Matlab’s memory-profiling options, we will see that the copy operation data2=data1 takes negligible time to run and allocates no memory […]

#4 Pingback By File deletion memory leaks, performance | Undocumented Matlab On September 5, 2012 @ 11:02 am

[…] To monitor Matlab’s Java heap space size in runtime, see my article from several months ago, or use Elmar Tarajan’s memory-monitor utility from the File Exchange. […]

#5 Pingback By Undocumented Profiler options part 2 | Undocumented Matlab On September 24, 2012 @ 3:11 am

[…] Since then, I have posted several articles about performance tuning and profiling (for example, memory profiling) but nothing specifically about the Profiler. […]

#6 Comment By Ian Schillebeeckx On November 29, 2012 @ 4:09 pm

What about:

profiler -memory on

?

#7 Comment By Yair Altman On November 29, 2012 @ 4:44 pm

@Ian – this was mentioned at the very top of this article:

…an undocumented feature was added to the Matlab Profiler that [22] memory usage.

#8 Pingback By Assessing Java object size in Matlab | Undocumented Matlab On January 29, 2014 @ 11:39 am

[…] a couple of years ago I posted an article on profiling Matlab’s memory usage, which included a section on Java memory […]


Article printed from Undocumented Matlab: https://undocumentedmatlab.com/blog

URL to article: https://undocumentedmatlab.com/blog/profiling-matlab-memory-usage

URLs in this post:

[1] Image: http://undocumentedmatlab.com/feed/

[2] email feed: http://undocumentedmatlab.com/subscribe_email.html

[3] enables monitoring: https://undocumentedmatlab.com/blog/undocumented-profiler-options/

[4] feature function: https://undocumentedmatlab.com/blog/undocumented-feature-function/

[5] memory manager: http://www.mathworks.com/support/tech-notes/1100/1106.html

[6] java.opts: http://www.mathworks.com/support/solutions/en/data/1-18I2C/

[7] and so on: http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html

[8] posted: http://www.mathworks.com/support/solutions/en/data/1-3L4JU7/

[9] JMP: http://www.khelekore.org/jmp/

[10] TIJMP: http://www.khelekore.org/jmp/tijmp/

[11] Classmexer: http://www.javamex.com/classmexer/

[12] sample usage: https://www.mathworks.com/matlabcentral/newsreader/view_thread/296813#797410

[13] Runtime: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Runtime.html

[14] Undocumented profiler options : https://undocumentedmatlab.com/blog/undocumented-profiler-options

[15] Datenum performance : https://undocumentedmatlab.com/blog/datenum-performance

[16] Undocumented Profiler options part 3 : https://undocumentedmatlab.com/blog/undocumented-profiler-options-part-3

[17] Undocumented Profiler options part 4 : https://undocumentedmatlab.com/blog/undocumented-profiler-options-part-4

[18] ismembc – undocumented helper function : https://undocumentedmatlab.com/blog/ismembc-undocumented-helper-function

[19] uiundo – Matlab’s undocumented undo/redo manager : https://undocumentedmatlab.com/blog/uiundo-matlab-undocumented-undo-redo-manager

[20] : http://www.mathworks.com/support/solutions/en/data/1-OVU1L/

[21] : https://www.mathworks.com/matlabcentral/newsreader/view_thread/155261

[22] : http://undocumentedmatlab.com/blog/undocumented-profiler-options/

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.