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

Assessing Java object size in Matlab

Posted By Yair Altman On January 29, 2014 | No Comments

Have you noticed that all Java object references are displayed as using 0 bytes in the Matlab Workspace browser and the whos function? This is not a bug, but in fact a deliberate design decision, in order to avoid the need to calculate the deep-memory usage of Java references (i.e., objects that include references to other objects etc.).

Well, sometimes it so happens that we really need to know the size of the Java object, or the size difference between two objects (to help resolve memory leaks, for example). There are several resources online that explain how to do this in Matlab (examples 1 [3], 2 [4], 3 [5]). Today I will show two alternatives that I found useful within the context of Matlab:

### ObjectProfiler

A full decade ago, Vladimir Roubtsov posted a very detailed article [8] in the JavaWorld magazine explaining how to profile and report Java object sizes. The article contained a downloadable Java archive [9] that we can easily use in Matlab. After downloading the zip file, extract the contained objectprofiler.jar file, add it to the Java classpath and start using it, as follows:

>> javaaddpath 'C:\path\to\where\you\placed\your\copy\of\objectprofiler.jar'   >> com.vladium.utils.ObjectProfiler.sizeof(java.awt.Color.red) ans = 28

Note that the reported sizes may be different on different Matlab releases (=JVM versions) and platforms. Also note that the reported size (28 bytes for the Java Color object) is much smaller than the size required to serialize the object [10] into a byte stream (408 bytes in this case), as I’ve shown in last week’s article.

Running the sizeof method on deeply referenced objects could quickly exhaust Matlab’s memory:

>> jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance; % on R2012a: takes a long time and finally reports >> com.vladium.utils.ObjectProfiler.sizeof(jDesktop) ans = 72011200 % on R2014a: takes a long time and finally croaks % (which is not surprising, considering the Desktop's new toolstrip) >> com.vladium.utils.ObjectProfiler.sizeof(jDesktop) Java exception occurred: java.lang.OutOfMemoryError: Java heap space at java.util.IdentityHashMap.resize(Unknown Source) at java.util.IdentityHashMap.put(Unknown Source) at com.vladium.utils.ObjectProfiler.computeSizeof(ObjectProfiler.java:329) at com.vladium.utils.ObjectProfiler.sizeof(ObjectProfiler.java:85) 

ObjectProfiler has a very handy feature of enabling a visual display of the object’s reference tree. For example:

>> jObject = java.util.Hashtable jObject = {} >> com.vladium.utils.ObjectProfiler.sizeof(jObject) ans = 105 >> com.vladium.utils.ObjectProfiler.profile(jObject).dump ans = 105 -> <INPUT> : Hashtable 60 (57.1%) -> Hashtable#table : Hashtable$Entry[] 60 (57.1%) -> <shell: Hashtable$Entry[], length=11> 45 (42.9%) -> <shell: 6 prim/4 ref fields> >> jObject.put('key1',1.23); >> com.vladium.utils.ObjectProfiler.sizeof(jObject) ans = 189 >> com.vladium.utils.ObjectProfiler.profile(jObject).dump ans = 189 -> <INPUT> : Hashtable 144 (76.2%) -> Hashtable#table : Hashtable$Entry[] 84 (44.4%) -> Hashtable#table[4] : Hashtable$Entry 44 (23.3%) -> Hashtable$Entry#key : String 24 (12.7%) -> String#value : char[] 24 (12.7%) -> <shell: char[], length=4> 20 (10.6%) -> <shell: 2 prim/1 ref fields> 24 (12.7%) -> <shell: 1 prim/3 ref fields> 16 (8.5%) -> Hashtable$Entry#value : Double 16 (8.5%) -> <shell: 1 prim/0 ref fields> 60 (31.7%) -> <shell: Hashtable\$Entry[], length=11> 45 (23.8%) -> <shell: 6 prim/4 ref fields> 

As we can see, adding the 'key1' key to the hashtable object actually added 2 new references: a 44-byte String and a 16-byte Double, plus 24 additional overhead bytes, for a total addition of 84 bytes.

ObjectProfiler has a convenience method sizedelta(jObject1,jObject2) which returns the size delta in bytes between the two specified objects. There are a few additional methods for ObjectProfiler and the ObjectProfiler.profile() object – interested readers are referred to the original article and to the source code (which is included within the zip file that we downloaded).

### Classmexer

The Classmexer utility [11] works a bit differently but is also very easy to use once the initial setup is done. First we need to download [12] the zip file, then extract the classmexer.jar and place it in your Matlab’s startup folder. In that same folder, edit (create if necessary) a java.opts file with the following line:

-javaagent:classmexer.jar

After restarting Matlab, we can use Classmexer as follows:

>> com.javamex.classmexer.MemoryUtil.deepMemoryUsageOf(java.awt.Color.red) ans = 32   >> jObject = java.util.Hashtable; >> com.javamex.classmexer.MemoryUtil.deepMemoryUsageOf(jObject) ans = 120 >> jObject.put('key1',1.23); jObject jObject = {key1=1.23} >> com.javamex.classmexer.MemoryUtil.deepMemoryUsageOf(jObject) ans = 264

Note how the values reported by Classmexer differ from those of ObjectProfiler. To tell the truth, I’m not sure which of them to believe: ObjectProfiler seems more detailed, but Classmexer uses Java’s preferred mechanism of using an instrumentation agent [13].

We can also use java.lang.Runtime.getRuntime‘s methods (maxMemory(), freeMemory() and totalMemory()) to monitor overall Java memory (note a MathWorks blog article [14] on this). Note that this reports the total memory values, and fluctuates (sometimes dramatically) from second to second, as Matlab’s desktop and other Java-heavy tools create Java objects, which the JVM garbage-collects.

Jeff Gullet has suggested [15] to monitor these values and programmatically activate a synchronous Java garbage-collection when the memory appears too “crowded” (I fixed Jeff’s posted idea in the snippet below):

r = java.lang.Runtime.getRuntime; if (r.freeMemory/r.totalMemory) < 0.1 r.gc(); end

A MathWorks technical article [16] provided some assistance on using the JConsole utility to profile Java memory in Matlab. We can also use the JMap and JHat utilities [17]. All these utilities are part of the free Java Development Kit (JDK) that can be downloaded online, just ensure you’re using the same Java version as reported by Matlab:

>> version -java ans = Java 1.7.0_11-b21 % i.e., Java 7 update 11

In addition to the JDK tools, I find the open-source JVisualVM [18] utility informative and easy to use. We can also use JMP [19] (R2007a and earlier), TIJMP [20] (R2007b and later), or other 3rd-party tools. A list of Java-centric resources is available in the Java SE Troubleshooting guide [21].

To complete the picture, a couple of years ago I posted an article on profiling Matlab’s memory usage [22], which included a section on Java memory. You may also find useful another article I wrote, on finding and fixing a Java memory leak in Matlab [23].

Categories: Java, Medium risk of breaking in future versions

URL to article: https://undocumentedmatlab.com/blog/assessing-java-object-size-in-matlab

URLs in this post:

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

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

[3] 1: http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object

[4] 2: http://stackoverflow.com/questions/757300/programatically-calculate-memory-occupied-by-a-java-object-including-objects-it

[5] 3: http://www.javapractices.com/topic/TopicAction.do?Id=83

[6] ObjectProfiler: https://undocumentedmatlab.com/blog/assessing-java-object-size-in-matlab/#ObjectProfiler

[7] Classmexer: https://undocumentedmatlab.com/blog/assessing-java-object-size-in-matlab/#Classmexer

[8] very detailed article: http://www.javaworld.com/javaworld/javaqa/2003-12/02-qa-1226-sizeof.html

[10] serialize the object: https://undocumentedmatlab.com/blog/serializing-deserializing-matlab-data/

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

[13] instrumentation agent: http://www.javamex.com/tutorials/memory/instrumentation.shtml

[14] blog article: http://blogs.mathworks.com/community/2009/08/17/calling-java-from-matlab-memory-issues/

[17] utilities: http://docs.oracle.com/javase/7/docs/technotes/tools/index.html

[18] JVisualVM: http://visualvm.java.net/

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

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

[21] Java SE Troubleshooting guide: http://www.oracle.com/technetwork/java/javase/index-138283.html

[22] profiling Matlab’s memory usage: https://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/

[23] fixing a Java memory leak in Matlab: https://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/

[24] JIDE Property Grids : https://undocumentedmatlab.com/blog/jide-property-grids

[25] Setting status-bar text : https://undocumentedmatlab.com/blog/setting-status-bar-text

[26] Figure toolbar customizations : https://undocumentedmatlab.com/blog/figure-toolbar-customizations

[27] The javacomponent function : https://undocumentedmatlab.com/blog/javacomponent

[28] uiinspect : https://undocumentedmatlab.com/blog/uiinspect

[29] Sliders in Matlab GUI : https://undocumentedmatlab.com/blog/sliders-in-matlab-gui