首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

shallow size & retained size 差异

2012-12-22 
shallow size & retained size 区别shallow size就是对象本身占用内存的大小,比如String对象public final

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字节

?

?

?

热点排行