转载

jvm之String 在jvm中分配

1、关于String的些许探讨

注意下面的案例:

/**   * JVM对于字符串常量的"+"号连接,将程序编译期,JVM就将常量字符串的"+"连接优化为连接后的值,拿"a" + 1来说,经编译器优化后在class中就已经是a1。在编译期其字符串常量的值就确定下来,故上面程序最终的结果都为true。   */  @Test  public void testConstant() {   if (true) {    String a = "a1";    String b = "a" + 1;    System.out.println((a == b)); // result = true   }   if (true) {    String a = "atrue";    String b = "a" + "true";    System.out.println((a == b)); // result = true   }   if (true) {    String a = "a3.4";    String b = "a" + 3.4;    System.out.println((a == b)); // result = true   }  }   /**   * JVM对于字符串引用,由于在字符串的"+"连接中,有字符串引用存在,而引用的值在程序编译期是无法确定的,即"a" + bb无法被编译器优化,只有在程序运行期来动态分配并将连接后的新地址赋给b。所以上面程序的结果也就为false。   */  @Test  public void testConstantAB() {   String a = "ab";   String bb = "b";   String b = "a" + bb;   System.out.println((a == b)); // result = false  }   /**   * 分析:和上面testConstantAB中唯一不同的是bb字符串加了final修饰,对于final修饰的变量,它在编译时被解析为常量值的一个本地拷贝存储到自己的常量池中或嵌入到它的字节码流中。所以此时的"a" + bb和"a" + "b"效果是一样的。故上面程序的结果为true。   */  @Test  public void testConstantFinalAB() {   String a = "ab";   final String bb = "b";   String b = "a" + bb;   System.out.println((a == b)); // result = true  }   /**   * JVM对于字符串引用bb,它的值在编译期无法确定,只有在程序运行期调用方法后,将方法的返回值和"a"来动态连接并分配地址为b,   * 故上面程序的结果为false。   */  @Test  public void testConstantFinalFunctionAB() {   String a = "ab";   final String bb = getBB();   String b = "a" + bb;   System.out.println((a == b)); // result = false  }   private static String getBB() {   return "b";  }

结论:

1、String a = "a1"; 变量a对应的字符串a1 分配在了PSPermGen的常量池。

证明思路:

jvm参数配置:-Xmx200M -Xms200M -Xmn100M -XX:SurvivorRatio=8   -XX:+PrintGCDetails -verbose:gc

java代码:

public static void main(String[] args) {   System.gc(); }

日志:

PSPermGen       total 21248K, used 3054K [0x00000000ee600000, 0x00000000efac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aaaaaaaaaaaaaaa";   System.gc();

日志:

PSPermGen       total 21248K, used 3057K [0x00000000ee600000, 0x00000000efac0000, 0x00000000f3800000)

证明 PSPermGen区由3054K增长了3K到3057K

2、String a = "ab"; final String bb = "b"; String b = "a" + bb; b同样分配在了常量区

证明思路:

public static void main(String[] args) {   System.gc();   final String s1= "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";   final String s4= "baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";   String s5 = s1 + s4;   System.gc();

PSPermGen       total 21248K, used 3060K [0x00000000ee600000, 0x00000000efac0000, 0x00000000f3800000)

证明:PSPermGen区由3057K->3060K,增长的依旧常量池。

3、String a = "ab"; String bb = "b"; String b = "a" + bb; b分配在了堆区。

证明思路:

方法1、调试 b = "a" + bb; 你会发现代码进入了StringBuilder

方法2如下:

public static void main(String[] args) {   System.gc(); }

日志:

PSYoungGen      total 92160K, used 3276K [0x00000000f9c00000, 0x0000000100000000, 0x0000000100000000)

eden space 81920K, 4% used [0x00000000f9c00000,0x00000000f9f333b0,0x00000000fec00000)

public static void main(String[] args) {   String s1= "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";   String s4= "baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";   long begin = Runtime.getRuntime().freeMemory();    for(int i=0;i<300;i++){    String temp =  s1+"-"+i+"-"+s4;    }      long end = Runtime.getRuntime().freeMemory();   System.out.println((begin - end)/1024 + "KB");  }

日志:

PSYoungGen      total 92160K, used 4916K [0x00000000f9c00000, 0x0000000100000000, 0x0000000100000000)

eden space 81920K, 6% used [0x00000000f9c00000,0x00000000fa0cd160,0x00000000fec00000)

对比说明eden 内存量在减少,说明"a" + bb 在jvm的存在是堆的eden区

正文到此结束
Loading...