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
然后写个test类
public class C {
private int x;
private int y;
public static void main(String [] args) {
System.out.println(ObjectSizeFetcher.getObjectSize(new C()));
}
}
把下面的类编译好放到ObjectSizeFetcherAgent.jar中
在MANIFEST.MF中加入:
然后写个test类
在命令行中这样执行: