实际上,博客文章通常表示,字符串池包含对字符串对象的引用,而实际的字符串对象是堆中的某个位置.也有非常混乱,无论永久代是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空间
@H_502_15@解决方法
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:
@H_502_15@ @H_502_15@ 原文链接:https://www.f2er.com/java/125337.htmlIn 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.