最小可运行示例
GitHub 上游:https://github.com/cirosantilli/java-cheat/tree/d73d2786cad458973a6b46bc98b9faabae65f3e1/instrument
元 INF/MANIFEST。中频
Premain-Class: Sizeof
大小.java
import java.lang.instrument.Instrumentation;
final public class Sizeof {
private static Instrumentation instrumentation;
public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}
public static long sizeof(Object o) {
return instrumentation.getObjectSize(o);
}
}
主要.java
final public class Main {
public static void main(String [] args) {
System.out.println("Object");
System.out.println(Sizeof.sizeof(new Object()));
System.out.println("/\"\"");
System.out.println(Sizeof.sizeof(""));
System.out.println("/\"abc\"");
System.out.println(Sizeof.sizeof("abc"));
System.out.println("int[0]");
System.out.println(Sizeof.sizeof(new int[0]));
System.out.println("int[10]");
System.out.println(Sizeof.sizeof(new int[10]));
class OneInt {
public int i;
}
System.out.println("OneInt");
System.out.println(Sizeof.sizeof(new OneInt()));
class TwoInts {
public int i;
public int j;
}
System.out.println("TwoInts");
System.out.println(Sizeof.sizeof(new TwoInts()));
class IntArray0 {
int[] i = new int[0];
}
System.out.println("IntArray0");
System.out.println(Sizeof.sizeof(new IntArray0()));
class IntArray10 {
int[] i = new int[10];
}
System.out.println("IntArray10");
System.out.println(Sizeof.sizeof(new IntArray10()));
}
}
制作文件
all:
javac *.java
jar -cfm Sizeof.jar META-INF/MANIFEST.MF Sizeof.class
java -ea -javaagent:Sizeof.jar Main
示例输出:
Object
16
/""
24
/"abc"
24
int[0]
16
int[10]
56
OneInt
16
TwoInts
24
IntArray0
16
IntArray10
16
在 Ubuntu 16.10、Java HotSpot 1.8.0_92 中测试。