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.
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
>> 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
>> 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 = java.lang.Runtime@5fb3b54 >> r.freeMemory ans = 86147768 >> r.totalMemory ans = 268304384 >> usedMemory = r.totalMemory - r.freeMemory;
>> 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
com.mathworks.xwidgets.JavaMemoryMonitor.invoke
Matlab R2011b's Java memory monitorAs 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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4