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/