2019独角兽企业重金招聘Python工程师标准>>>
首先,将下面的类编译并放入jar中:
import java.lang.instrument.Instrumentation;
public class ObjectSizeFetcher {
private static Instrumentation instrumentation;
public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}
public static long getObjectSize(Object o) {
return instrumentation.getObjectSize(o);
}
}
在MANIFEST.MF中添加如下内容:
Premain-Class: ObjectSizeFetcher
public class C {
private int x;
private int y;
public static void main(String [] args) {
System.out.println(ObjectSizeFetcher.getObjectSize(new C()));
}
}
java -javaagent:ObjectSizeFetcherAgent.jar C
更多:
Calculate size of Object in Java: http://stackoverflow.com/questions/9368764/calculate-size-of-object-in-java
In Java, what is the best way to determine the size of an object?: http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object
JavaAgent: http://www.cnblogs.com/diyunpeng/archive/2011/05/26/2057932.html
jamm: https://github.com/jbellis/jamm