1.
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); } }
2.
jar cvf ObjectSizeFetcher.jar ObjectSizeFetcher.class
3.
Add the following to your MANIFEST.MF
:
Premain-Class: ObjectSizeFetcher
4.
public class C { private int x; private int y; public static void main(String [] args) { System.out.println(ObjectSizeFetcher.getObjectSize(new C())); } }
5.调用
java -javaagent:ObjectSizeFetcherAgent.jar C
时间: 2024-10-16 19:32:59