- 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 [1], 2 [2], 3 [3]). Today I will show two alternatives that I found useful within the context of Matlab:


A full decade ago, Vladimir Roubtsov posted a very detailed article [6] in the JavaWorld magazine explaining how to profile and report Java object sizes. The article contained a downloadable Java archive [7] 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 =

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 [8] 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 =
% 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 =
>> 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 =
>> 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).


The Classmexer utility [9] works a bit differently but is also very easy to use once the initial setup is done. First we need to download [10] 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:


After restarting Matlab, we can use Classmexer as follows:

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

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 [11].

We can also use java.lang.Runtime.getRuntime‘s methods (maxMemory(), freeMemory() and totalMemory()) to monitor overall Java memory (note a MathWorks blog article [12] 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 [13] 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

A MathWorks technical article [14] provided some assistance on using the JConsole utility to profile Java memory in Matlab. We can also use the JMap and JHat utilities [15]. 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 [16] utility informative and easy to use. We can also use JMP [17] (R2007a and earlier), TIJMP [18] (R2007b and later), or other 3rd-party tools. A list of Java-centric resources is available in the Java SE Troubleshooting guide [19].
To complete the picture, a couple of years ago I posted an article on profiling Matlab’s memory usage [20], 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 [21].

Categories: Java, Medium risk of breaking in future versions

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

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

URLs in this post:

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

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

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

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

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

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

[7] downloadable Java archive: http://images.techhive.com/downloads/idge/imported/article/jvw/2003/12/02-qa-1226-sizeof.zip

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

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

[10] download: http://www.javamex.com/classmexer/classmexer-0_03.zip

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

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

[13] suggested: https://www.mathworks.com/matlabcentral/newsreader/view_thread/296813#797410

[14] technical article: http://www.mathworks.com/matlabcentral/answers/95990

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

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

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

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

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

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

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

[22] FindJObj – find a Matlab component's underlying Java object : https://undocumentedmatlab.com/articles/findjobj-find-underlying-java-object

[23] Matlab callbacks for Java events in R2014a : https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events-in-r2014a

[24] Matlab callbacks for Java events : https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events

[25] Using pure Java GUI in deployed Matlab apps : https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps

[26] Matlab-Java memory leaks, performance : https://undocumentedmatlab.com/articles/matlab-java-memory-leaks-performance

[27] New book: Undocumented Secrets of MATLAB-Java Programming : https://undocumentedmatlab.com/articles/matlab-java-book

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