java计算对象占用内存大小 SizeOf
Quickstart
1. include sizeOf.jar in the classpath of your application and use it in your code like this:
import net.sourceforge.sizeof ... SizeOf.skipStaticField(true); //java.sizeOf will not compute static fields SizeOf.skipFinalField(true); //java.sizeOf will not compute final fields SizeOf.skipFlyweightObject(true); //java.sizeOf will not compute well-known flyweight objects System.out.println(SizeOf.deepSizeOf(<your object>)); //this will print the object size in bytes
You can dump object's size setting the min size to log
(if you don't specify an output stream standard out is used):
SizeOf.setMinSizeToLog(1024); //min object size to log in bytes SizeOf.setLogOutputStream(new FileOutputStream("<your log file>"));
Use the humanReadable() method to get the object size in byte, kilo or mega
(if you need giga your in trouble guy!):
SizeOf.humanReadable(SizeOf.deepSizeOf(<your object>));
2. start your application with the following JVM parameter:
-javaagent:/path_to/sizeOf.jar
3.在项目中引入SizeOf.jar
4.在VM arguments中加入 -javaagent:"F:\workspace\jtest\lib\SizeOf.jar"