在java里有没有想delphi的sizeof()的功能?(50分)

  • 主题发起人 onedolph
  • 开始时间
O

onedolph

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么知道一个对象的尺寸?用什么方法?
 
class MemoryTest {
protected static final long COUNT = 100;

public static void main(String[] arg) {
long start, end, difference;
Object[] array = new Object[COUNT];
long i;

Runtime.getRuntime.gc();
// let's hope the
// garbage collector runs
start = Runtime.getRuntime().totalMemory();
for (i = 0;
i < COUNT;
i++) {
array = new Object();
}
Runtime.getRuntime.gc();
end = Runtime.getRuntime().totalMemory();

difference = (end - start) / COUNT;

System.out.println("Approximately " + difference
+ " bytes used by 1 java.lang.Object with
default constructor");
}
}
 
这样好烦啊,难道就没有sizeof()了?
 
这是在java.sun.com上找到的。
 
没有的.
因为sizeof()在不同的机器上,可能的到不同的数量,所以sun的java 为拉.......
结论:没有

 
对象大小跟VM有关,其实知道了也没多大用
 
将对象写到一个文件里面,看看文件有多大。。。
好像没有你要的函数。
 
多人接受答案了。
 

Similar threads

顶部