2012-11-14 4 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에서 인턴 된 문자열은 영구 생성에 더 이상 저장되지 않습니다. 그것들은 당신이 생성 할 다른 객체들처럼 힙의 주요 부분에 저장됩니다.

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

예외는 힙에있는 배열의 생성 때문입니다. "permgen 메모리 부족"오류가 발생하면 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

Java 7의 경우 +1 –

+0

+1을 참조하십시오. –

+0

'Java.lang.OutOfMemoryError : Java heap space' -하지만 자바 6 업데이트 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()