Compile the following class and put it in ObjectSizeFetcherAgent.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);
}
}
Add to MANIFEST.MF:
Premain-Class: ObjectSizeFetcher
Then write a test class
public class C {
private int x;
private int y;
public static void main(String [] args) {
System.out.println(ObjectSizeFetcher.getObjectSize(new C()));
}
}
Compile the following class and put it in ObjectSizeFetcherAgent.jar
Add to MANIFEST.MF:
Then write a test class
Execute like this in the command line: