什么方法实现像C++中的sizeof() 函数呢 在Java中?
我们知道 在java中 所有的这些简单类型,无论在什么平台 它们的字节空间
占用都是一样的
但是我仍然想知道,我自己义的一个对象类型 占用多少内存
我如何实现呢?
问题点数:100、回复次数:11Top
1 楼killme2008(为了生态平衡,请保护蛤蟆)回复于 2003-12-03 12:45:40 得分 0
好象不行吧
如果是数据结构还可以Top
2 楼xiaohaiz(城里的老土,两眼依然通红!)回复于 2003-12-03 13:00:00 得分 10
呵呵,也不能说是完全没有办法。如果你的类实现了序列化接口,你就可以把对象写到字节流,然后数一下字节流的长度,就能估测了。举例如下:
<< 假设Data类为数据结构类。
class Data implements Serializable {
int forTest;
}
>>
这样获取它的长度:
<<
Data d1 = new Data();
d1.forTest = 99;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(d1);
oos.close();
int len = bos.toByteArray().length;
>>
上例中,获取的长度是39个字节,除了最后的4个字节是int数据之外,还有35个字节代表了其他的信息。这里就不深入了。Top
3 楼kypfos(不在寻梦)回复于 2003-12-03 13:05:05 得分 0
这样是不是准确的呢?Top
4 楼xiaohaiz(城里的老土,两眼依然通红!)回复于 2003-12-03 13:16:34 得分 10
<<估测>>
真正对象所占据的内存,不进入到vm以下是无法得知的。内存的分配都是vm通过本地方法来分配的。不同的虚拟机实现做法也不同。在vmspec中关于这点也没有严格的定义。所以对于上层来说,获取内存空间的大小是没有意义的。
Top
5 楼Shrewdcat(丧邦&灵猫&潇)回复于 2003-12-03 13:27:21 得分 0
java 屏弃c++的sizeof ,是因为在java中实现了跨平台开发,也就是说,太不同的系统中,这些基本变量的字节是固定不变的,所以,sizeof 也就没有存在的必要了。
也就是说,sizeof是不被java所支持的功能。Top
6 楼nanman(南蛮—500万人使用的最强五笔作者)回复于 2003-12-03 13:48:07 得分 30
import java.util.*;
/**
* Utility class for determining the sizes in bytes of commonly used objects.
* Classes implementing the Cacheable interface should use this class to
* determine their size.
*
* @author Matt Tucker
*/
public class CacheSizes {
/**
* Returns the size in bytes of a basic Object. This method should only
* be used for actual Object objects and not classes that extend Object.
*
* @return the size of an Object.
*/
public static int sizeOfObject() {
return 4;
}
/**
* Returns the size in bytes of a String.
*
* @param string the String to determine the size of.
* @return the size of a String.
*/
public static int sizeOfString(String string) {
if (string == null) {
return 0;
}
return 4 + string.length()*2;
}
/**
* Returns the size in bytes of a primitive int.
*
* @return the size of a primitive int.
*/
public static int sizeOfInt() {
return 4;
}
/**
* Returns the size in bytes of a primitive char.
*
* @return the size of a primitive char.
*/
public static int sizeOfChar() {
return 2;
}
/**
* Returns the size in bytes of a primitive boolean.
*
* @return the size of a primitive boolean.
*/
public static int sizeOfBoolean() {
return 1;
}
/**
* Returns the size in bytes of a primitive long.
*
* @return the size of a primitive long.
*/
public static int sizeOfLong() {
return 8;
}
/**
* Returns the size in bytes of a primitive double.
*
* @return the size of a primitive double.
*/
public static int sizeOfDouble() {
return 8;
}
/**
* Returns the size in bytes of a Date.
*
* @return the size of a Date.
*/
public static int sizeOfDate() {
return 12;
}
/**
* Returns the size in bytes of a Map object. All keys and
* values <b>must be Strings</b>.
*
* @param map the Map object to determine the size of.
* @return the size of the Map object.
*/
public static int sizeOfMap(Map map) {
if (map == null) {
return 0;
}
// Base map object -- should be something around this size.
int size = 36;
// Add in size of each value
Object [] values = map.values().toArray();
for (int i=0; i<values.length; i++) {
size += sizeOfString((String)values[i]);
}
Object [] keys = map.keySet().toArray();
// Add in each key
for (int i=0; i<keys.length; i++) {
size += sizeOfString((String)keys[i]);
}
return size;
}
/**
* Returns the size in bytes of a List object. All elements
* <b>must be Strings</b>.
*
* @param list the List object to determine the size of.
* @return the size of the List object.
*/
public static int sizeOfList(List list) {
if (list == null) {
return 0;
}
// Base list object (approximate)
int size = 36;
// Add in size of each value
Object [] values = list.toArray();
for (int i=0; i<values.length; i++) {
size += sizeOfString((String)values[i]);
}
return size;
}
}
Top
7 楼killme2008(为了生态平衡,请保护蛤蟆)回复于 2003-12-03 14:08:52 得分 0
to xiaohaiz(老土进城,两眼通红)
经常看到你,很强,佩服,基础很牢固
我还是初学的,向您学习
能不能交个朋友
QQ:94453310
Top
8 楼xiaohaiz(城里的老土,两眼依然通红!)回复于 2003-12-03 14:18:24 得分 0
TO killme2008(我不会编程):
Sorry,俺没有QQ,只有MSN : xiaohaiz@21cn.comTop
9 楼wellingsok(不及格的程序员)回复于 2003-12-03 15:49:15 得分 50
markTop
10 楼drinkant(喝酒的蚂蚁)回复于 2003-12-03 15:56:57 得分 0
remark :-)Top
11 楼killme2008(为了生态平衡,请保护蛤蟆)回复于 2003-12-04 13:00:50 得分 0
楼主也太。。。。。
老土回答的这么辛苦才给那几分啊
那wellingsok (50)是不是你自己啊Top




