呵呵,也不能说是完全没有办法。如果你的类实现了序列化接口,你就可以把对象写到字节流,然后数一下字节流的长度,就能估测了。举例如下:
<< 假设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个字节代表了其他的信息。这里就不深入了。
<< 假设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个字节代表了其他的信息。这里就不深入了。
真正对象所占据的内存,不进入到vm以下是无法得知的。内存的分配都是vm通过本地方法来分配的。不同的虚拟机实现做法也不同。在vmspec中关于这点也没有严格的定义。所以对于上层来说,获取内存空间的大小是没有意义的。
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;
}
}
Sorry,俺没有QQ,只有MSN : [email protected]
老土回答的这么辛苦才给那几分啊
那wellingsok (50)是不是你自己啊