When we say that interned strings are stored in permanent generation area then does the same applies for string literals also? Or it is only for strings interned by inter()?
Actually blog posts usually say that string pool contains reference to string object while actual string object is somewhere in heap. also there is very much confusion that whether permanent generation is IN heap or outside of it. (i used jcosole it is showing permanent gen different from heap.many posts say it as a part of heap and many say it is different)
Edit:
Also when I ran:
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++;
}
}
}
I was expecting Java.lang.OutOfMemoryError: PermGen space But i got :
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)
Shouldn’t it be Java.lang.OutOfMemoryError: PermGen space
Literal strings are interned. So yes, in Java 6-.
From Java 7, interned strings are not stored in permanent generation any longer. They are stored in the main part of the heap like any other objects you would create.
The exception you get is caused by the creation of an array which lives on the heap. To try to get an “out of permgen memory” error, you could try to remove the
list.add()lines. Note however that interned strings can be garbage collected so even doing that will still not cause the exception you expect.Cf RFE 6962931: