获取普通Java对象大小

缓存对象需要知道对象占用空间的大小,可以事先设置好每种类型的大小,此方法对普通的对象起效,Jive论坛中的对象也是采用这种办法来获取对象的大小的(取自Jive).

 

  1. public class CacheSizes {  
  2.     /** 
  3.      * Returns the size in bytes of a basic Object. This method should only 
  4.      * be used for actual Object objects and not classes that extend Object. 
  5.      * 
  6.      * @return the size of an Object. 
  7.      */  
  8.     public static int sizeOfObject() {  
  9.         return 4;  
  10.     }  
  11.     /** 
  12.      * Returns the size in bytes of a String. 
  13.      * 
  14.      * @param string the String to determine the size of. 
  15.      * @return the size of a String. 
  16.      */  
  17.     public static int sizeOfString(String string) {  
  18.         if (string == null) {  
  19.             return 0;  
  20.         }  
  21.         return 4 + string.length()*2;  
  22.     }  
  23.     /** 
  24.      * Returns the size in bytes of a primitive int. 
  25.      * 
  26.      * @return the size of a primitive int. 
  27.      */  
  28.     public static int sizeOfInt() {  
  29.         return 4;  
  30.     }  
  31.     /** 
  32.      * Returns the size in bytes of a primitive char. 
  33.      * 
  34.      * @return the size of a primitive char. 
  35.      */  
  36.     public static int sizeOfChar() {  
  37.         return 2;  
  38.     }  
  39.     /** 
  40.      * Returns the size in bytes of a primitive boolean. 
  41.      * 
  42.      * @return the size of a primitive boolean. 
  43.      */  
  44.     public static int sizeOfBoolean() {  
  45.         return 1;  
  46.     }  
  47.     /** 
  48.      * Returns the size in bytes of a primitive long. 
  49.      * 
  50.      * @return the size of a primitive long. 
  51.      */  
  52.     public static int sizeOfLong() {  
  53.         return 8;  
  54.     }  
  55.     /** 
  56.      * Returns the size in bytes of a primitive double. 
  57.      * 
  58.      * @return the size of a primitive double. 
  59.      */  
  60.     public static int sizeOfDouble() {  
  61.         return 8;  
  62.     }  
  63.     /** 
  64.      * Returns the size in bytes of a Date. 
  65.      * 
  66.      * @return the size of a Date. 
  67.      */  
  68.     public static int sizeOfDate() {  
  69.         return 12;  
  70.     }  
  71.     /** 
  72.      * Returns the size in bytes of a Properties object. All properties and 
  73.      * property names must be Strings. 
  74.      * 
  75.      * @param properties the Properties object to determine the size of. 
  76.      * @return the size of a Properties object. 
  77.      */  
  78.     public static int sizeOfProperties(Properties properties) {  
  79.         if (properties == null) {  
  80.             return 0;  
  81.         }  
  82.         //Base properties object  
  83.         int size = 36;  
  84.         //Add in size of each property  
  85.         Enumeration enum = properties.elements();  
  86.         while(enum.hasMoreElements()) {  
  87.             String prop = (String)enum.nextElement();  
  88.             size += sizeOfString(prop);  
  89.         }  
  90.         //Add in property names  
  91.         enum = properties.propertyNames();  
  92.         while(enum.hasMoreElements()) {  
  93.             String prop = (String)enum.nextElement();  
  94.             size += sizeOfString(prop);  
  95.         }  
  96.         return size;  
  97.     }  
  98.     /** 
  99.      * Returns the size in bytes of a Map object. All keys and 
  100.      * values <b>must be Strings</b>. 
  101.      * 
  102.      * @param map the Map object to determine the size of. 
  103.      * @return the size of the Map object. 
  104.      */  
  105.     public static int sizeOfMap(Map map) {  
  106.         if (map == null) {  
  107.             return 0;  
  108.         }  
  109.         //Base map object -- should be something around this size.  
  110.         int size = 36;  
  111.         //Add in size of each value  
  112.         Iterator iter = map.values().iterator();  
  113.         while(iter.hasNext()) {  
  114.             String value = (String)iter.next();  
  115.             size += sizeOfString(value);  
  116.         }  
  117.         //Add in each key  
  118.         iter = map.keySet().iterator();  
  119.         while(iter.hasNext()) {  
  120.             String key = (String)iter.next();  
  121.             size += sizeOfString(key);  
  122.         }  
  123.         return size;  
  124.     }  
  125. }  

点击打开链接
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值