shallow size 和 retained size

shallow size就是对象本身占用内存的大小,比如String对象

public final class String

    implements java.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 hash code 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字节

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22111412/viewspace-624519/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/22111412/viewspace-624519/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值