专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 人工智能

shallow size & retained size 差异

发布时间:2011-07-03 08:23:31 文章来源:www.iduyao.cn 采编人员:星星草
shallow size & retained size 区别

shallow size就是对象本身占用内存的大小,比如String对象
public final class String
   implementsjava.io.Serializable, Comparable<String>, CharSequence
{
   /**The value is used for character storage.*/
   private final char value[];
 
   /**The offset is the first index of the storage that is used.*/
   private final int offset;
 
   /**The count is the number of characters in the String.*/
   private final int count;
 
   /**Cache the hashcode for the string*/
 private int hash;// Default to 0
。。。


32位的操作系统每个对象头占8个字节,有三个int属性,每个int占用4个字节
有一个char[]对象,这个对象无论有没有赋值,都有个null值,所以也占4个字节
所以每个String对象的shallow size,占用: 3*4 + 1*4 + 8 = 24个字节
很显然String如果有值,比如String str = new String(“123”);,那该String对象总共占用的字节是24+的,
这就要说retained size了
 
retained size:当该对象的内存释放后,GC总共能回收的内存,其实就是shallow size+reference object size
比如String str = new String(“a”);
str对象的shallow size = 24
char[]对象的shallow size =8
char[]value的内存空间2*1=2
str对象的retained size = 24+8+2 = 34字节

 

 

 

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: