Java对象的大小

基本数据的类型的大小是固定的,这里就不多说了。对于非基本类型的Java对象,其大小就值得商榷。

在Java中,一个空Object对象的大小是8byte,这个大小只是保存堆中一个没有任何属性的对象的大小。看下面语句:

  Object ob = new Object();

这样在程序中完成了一个Java对象的生命,但是它所占的空间为:4byte+8byte。4byte是上面部分所说的Java栈中保存引用的 所需要的空间。而那8byte则是Java堆中对象的信息。因为所有的Java非基本类型的对象都需要默认继承Object对象,因此不论什么样的 Java对象,其大小都必须是大于8byte。

有了Object对象的大小,我们就可以计算其他对象的大小了。

Class NewObject { int count; boolean flag; Object ob; }

其大小为:空对象大小(8byte)+int大小(4byte)+Boolean大小(1byte)+空Object引用的大小 (4byte)=17byte。但是因为Java在对对象内存分配时都是以8的整数倍来分,因此大于17byte的最接近8的整数倍的是24,因此此对象 的大小为24byte。

这里需要注意一下基本类型的包装类型的大小。因为这种包装类型已经成为对象了,因此需要把他们作为对象来看待。包装类型的大小至少是 12byte(声明一个空Object至少需要的空间),而且12byte没有包含任何有效信息,同时,因为Java对象大小是8的整数倍,因此一个基本 类型包装类的大小至少是16byte。这个内存占用是很恐怖的,它是使用基本类型的N倍(N>2),有些类型的内存占用更是夸张(随便想下就知道 了)。因此,可能的话应尽量少使用包装类。在JDK5.0以后,因为加入了自动类型装换,因此,Java虚拟机会在存储方面进行相应的优化。

public class CacheSize { 
     /**
      * 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 Properties object. All properties and
      * property names must be Strings.
      *
      * @param properties the Properties object to determine the size of.
      * @return the size of a Properties object.
      */ 
     public static int sizeOfProperties(Properties properties) { 
         if (properties == null) { 
             return 0; 
         } 
         //Base properties object 
         int size = 36; 
         //Add in size of each property 
         Enumeration enum = properties.elements(); 
         while(enum.hasMoreElements()) { 
             String prop = (String)enum.nextElement(); 
             size += sizeOfString(prop); 
         } 
         //Add in property names 
         enum = properties.propertyNames(); 
         while(enum.hasMoreElements()) { 
             String prop = (String)enum.nextElement(); 
             size += sizeOfString(prop); 
         } 
         return size; 
     } 
     /**
      * 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 
         Iterator iter = map.values().iterator(); 
         while(iter.hasNext()) { 
             String value = (String)iter.next(); 
             size += sizeOfString(value); 
         } 
         //Add in each key 
         iter = map.keySet().iterator(); 
         while(iter.hasNext()) { 
             String key = (String)iter.next(); 
             size += sizeOfString(key); 
         } 
         return size; 
     } 
 }

猜你喜欢

转载自tomyz0223.iteye.com/blog/1109871