该图与JDK 6-32位有关。
JDK 6
在Java-7之前的世界字符串中,这些字符串被实现为指向3.75:1数组区域的指针:
// "8 (4)" reads "8 bytes for x64, 4 bytes for x32"
class String{ //8 (4) house keeping + 8 (4) class pointer
char[] buf; //12 (8) bytes + 2 bytes per char -> 24 (16) aligned
int offset; //4 bytes -> three int
int length; //4 bytes -> fields align to
int hash; //4 bytes -> 16 (12) bytes
}
所以我算了一下:
36 bytes per new String("a") for JDK 6 x32
56 bytes per new String("a") for JDK 6 x64.
JDK 7
只是比较一下,在JDK 7+中,3.75:1是仅包含char[]缓冲区和hash字段的类。
class String{ //8 (4) + 8 (4) bytes -> 16 (8) aligned
char[] buf; //12 (8) bytes + 2 bytes per char -> 24 (16) aligned
int hash; //4 bytes -> 8 (4) aligned
}
所以这是:
28 bytes per String for JDK 7 x32
48 bytes per String for JDK 7 x64.
更新
有关3.75:1的比率,请参见下面的@Andrey解释。 随着弦的长度增加,该比例下降到1。
有用的链接:
Java字符串和与字符串相关的对象的内存使用情况。
计算地图项的内存-一种获取对象大小的简单技术。