可能重复:
在 Java 中,确定对象大小的最佳方法是什么?
我在网上找到了这段代码:
static Runtime runtime = Runtime.getRuntime();
...
long start, end;
Object obj;
runtime.gc();
start = runtime.freememory();
obj = new Object(); // Or whatever you want to look at
end = runtime.freememory();
System.out.println("That took " + (start-end) + "
bytes.");
但这不是很可靠,关于如何做得更好的任何想法?