Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JVM getObjectSize example

Tags:

java

jvm

I have some trouble on getting the size of an object in JVM. JDK 5 offer an interface Instrumentation to calculate through premain() function, but I do not know how to use it.

Can anybody help me?

like image 608
fleture Avatar asked Jul 30 '12 09:07

fleture


1 Answers

the simplest and the most efficient solution is to use very small sizeof library

<dependency>
    <groupId>com.carrotsearch</groupId>
    <artifactId>java-sizeof</artifactId>
    <version>0.0.3</version>
</dependency>

you need to use this http://grepcode.com/file/repo1.maven.org/maven2/com.carrotsearch/java-sizeof/0.0.2/com/carrotsearch/sizeof/RamUsageEstimator.java?av=f

shallowSizeOf() method or sizeOf()

That's really the simplest working fast solution i found on this topic so far...

like image 147
Andrey Borisov Avatar answered Oct 10 '22 23:10

Andrey Borisov