2012-11-14 14 views
10

永続生成領域に格納された文字列が格納されているとすると、同じことが文字列リテラルにも適用されますか?それとも、inter()によってインターンされた文字列の場合のみですか?文字列リテラルと永久生成メモリ領域

実際にはブログの投稿では文字列プールに文字列オブジェクトへの参照が含まれていると言われていますが、実際の文字列オブジェクトにはヒープのがあります。。また、永続的な世代がヒープであるかそれ以外であるかは非常に混乱しています。 (私はそれがheap.manyの記事から永久世代の異なる表示されjcosole使用は、ヒープの一部としてそれを言うと、多くは、それは違うと言う)

編集:また 私は走った:

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 spaceを期待していた。しかし、私は得た:

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 space

答えて

6

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から、interned文字列は永久生成に保存されていません。それらは、作成する他のオブジェクトと同様に、ヒープの主要部分に格納されます。

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

例外は、ヒープ上に存在する配列の作成が原因です。 "permgen memory"エラーを取得しようとすると、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.

+0

+1のJava 7の事実 –

+0

+1参考です。 –

+0

'Java.lang.OutOfMemoryError:Java heap space' - しかし、私はJava 6 update 29を使用しています。だから、' Java.lang.OutOfMemoryError:PermGen space'を取得する必要があります。 –

2

String JavaDocで説明するように、すべての文字列リテラルは、自動的に抑留されています

All literal strings and string-valued constant expressions are interned.

私は行動が手動でインターンと任意の文字列リテラル文字列の間で一致するように期待します。

+0

はい私もそうだと思います。そうでなければ、彼らはどこかに文書化したでしょう。 –

0

s.intern()はインターン文字列への参照を返しますが、あなたはそれを使用していません。 試着s = s.intern()

関連する問題