2012-11-14 8 views
10

जब हम कहते हैं कि आंतरिक पीढ़ी स्थायी पीढ़ी क्षेत्र में संग्रहीत हैं तो क्या यह स्ट्रिंग अक्षर के लिए भी लागू होता है? या यह केवल इंटर() द्वारा प्रशिक्षित तारों के लिए है?स्ट्रिंग अक्षर और स्थायी पीढ़ी मेमोरी एरिया

असल में ब्लॉग पोस्ट आमतौर पर कहते हैं कि स्ट्रिंग पूल में स्ट्रिंग ऑब्जेक्ट का संदर्भ होता है जबकि वास्तविक स्ट्रिंग ऑब्जेक्ट हैप में है। यह भी बहुत भ्रम है कि स्थायी पीढ़ी ढेर में या उसके बाहर है या नहीं। (I 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 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?

शाब्दिक तारों को प्रशिक्षित किया जाता है। तो हाँ, जावा 6- में।

जावा 7 से, आंतरिक स्ट्रिंग स्थायी पीढ़ी में अब संग्रहित नहीं हैं। वे ढेर के मुख्य भाग में संग्रहीत होते हैं जैसे कि आप बनाएंगे।

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

आपको प्राप्त अपवाद एक सरणी के निर्माण के कारण होता है जो ढेर पर रहता है। "Permgen स्मृति से बाहर" त्रुटि प्राप्त करने का प्रयास करने के लिए, आप list.add() लाइनों को निकालने का प्रयास कर सकते हैं। ध्यान दें कि आंतरिक तारों को कचरा इकट्ठा किया जा सकता है, फिर भी ऐसा करने से आप अपवाद का कारण नहीं बनेंगे।

सीएफ़ 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। –

+0

+1 के लिए –

+0

'Java.lang.OutOfMemoryError: जावा हीप स्पेस '- लेकिन मैं जावा 6 अपडेट 29 का उपयोग कर रहा हूं। इसलिए मुझे' Java.lang.OutOfMemoryError: PermGen space' –

2

String JavaDoc में वर्णित के रूप सभी स्ट्रिंग शाब्दिक, स्वचालित रूप से प्रशिक्षु रहे हैं:

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

मैं व्यवहार तार आप मैन्युअल रूप से प्रशिक्षु और किसी भी स्ट्रिंग शाब्दिक के बीच संगत होने की अपेक्षा करेंगे।

+0

हाँ मैं भी ऐसा सोचता हूं। व्यवहार सुसंगत रहेगा अन्यथा वे इसे कहीं भी दस्तावेज करेंगे। संदर्भ के लिए जावा 7 तथ्यों –

0

s.intern() प्रशिक्षु स्ट्रिंग के लिए एक संदर्भ रिटर्न लेकिन आप इसे का उपयोग नहीं कर रहे हैं। s = s.intern()

संबंधित मुद्दे