Java – string literals and permanently generated memory areas

When we say that the actual string is stored in the permanent generation area, the same applies to string text? Or is it just a string detained by inter()?

In fact, blog posts usually indicate that the string pool contains references to string objects, and the actual string object is somewhere in the heap There is also a lot of confusion, whether the permanent generation is in or outside it (I use jcosole, which shows that the eternal root is different from the heap. Many posts say it is part of the heap, and many people say it is different)

Editor: when I run

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 look forward to Java Lang. outofmemoryerror: permgen space, but I get:

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)

It shouldn't be Java Lang. outofmemoryerror: permgen space

Solution

The text string is detained So yes, in Java 6 -

Starting with Java 7, internal strings are no longer stored in permanent code They are stored in the main part of the heap like any other object you create

The exception you get is caused by the creation of an array generated on the heap To try to get the "out of memory" error, you can try deleting the list Add() line Note, however, that the inner string may be garbage collected, so even doing so will not cause the exception you expect

Cf RFE 6962931:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>