最小実行可能例えば上流
GitHubの:https://github.com/cirosantilli/java-cheat/tree/d73d2786cad458973a6b46bc98b9faabae65f3e1/instrument
META-INF/MANIFEST.MF:
Premain-Class: Sizeof
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);
}
}
Main.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()));
}
}
のMakefile:
all:
javac *.java
jar -cfm Sizeof.jar META-INF/MANIFEST.MF Sizeof.class
java -ea -javaagent:Sizeof.jar Main
出力例:Ubuntuの16.10、は、Java HotSpot 1.8.0_92でテスト
Object
16
/""
24
/"abc"
24
int[0]
16
int[10]
56
OneInt
16
TwoInts
24
IntArray0
16
IntArray10
16
。
質問に対する回答は、[このページ](http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/package-summary.html)にあります。 –