加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 字符串文字和永久生成内存区域

发布时间:2020-12-14 16:27:37 所属栏目:Java 来源:网络整理
导读:当我们说实际的字符串存储在永久生成区域中时,同样适用于字符串文字?或者只是被inter()所拘留的字符串? 实际上,博客文章通常表示,字符串池包含对字符串对象的引用,而实际的字符串对象是堆中的某个位置.也有非常混乱,无论永久代是IN堆还是在它之外. (我使用
当我们说实际的字符串存储在永久生成区域中时,同样适用于字符串文字?或者只是被inter()所拘留的字符串?

实际上,博客文章通常表示,字符串池包含对字符串对象的引用,而实际的字符串对象是堆中的某个位置.也有非常混乱,无论永久代是IN堆还是在它之外. (我使用jcosole它显示永恒根不同于heap.many帖子说它作为堆的一部分,许多人说它是不同的)

编辑:
当我跑的时候

public class stringtest2{
  public static void main(String args[]){
    int i=0;
    List<String> list=new ArrayList<String>();
    while(true){
      String s="hello"+i;
      String s1=i+"hello";
      String s2=i+"hello"+i;
      System.out.println(s);
      s.intern();

      s1.intern();
      s2.intern();
      list.add(s);
      list.add(s1);
      list.add(s2);
      i++;
    }
  }
}

我期待Java.lang.OutOfMemoryError:PermGen空间但我得到:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at java.util.Arrays.copyOf(Arrays.java:2760)
        at java.util.Arrays.copyOf(Arrays.java:2734)
        at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
        at java.util.ArrayList.add(ArrayList.java:351)
        at stringtest2.main(stringtest2.java:20)

不应该是Java.lang.OutOfMemoryError:PermGen空间

解决方法

When we say that interned strings are stored in permanent generation area then does the same applies for string literals also?

文字字符串被扣留.所以是的,在Java 6-.

从Java 7开始,内部的字符串不再存储在永久代码中.它们像您创建的任何其他对象一样存储在堆的主要部分中.

Shouldn’t it be Java.lang.OutOfMemoryError: PermGen space

您获得的异常是由生成在堆上的数组的创建引起的.要尝试获取“超出内存”错误,您可以尝试删除list.add()行.但是请注意,内部的字符串可能会被垃圾回收,所以即使这样做仍然不会导致您期望的异常.

Cf RFE 6962931:

In JDK 7,interned strings are no longer allocated in the permanent generation of the Java heap,but are instead allocated in the main part of the Java heap (known as the young and old generations),along with the other objects created by the application. This change will result in more data residing in the main Java heap,and less data in the permanent generation,and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change,but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读