pranav
pranav

Reputation: 431

String creation in Pool and Heap

Have few doubts in String,

I may be wrong with certain statements as i am writing this based on my understanding from various articles on Internet, please bear with me.

  1. When we do String str1 = new String ("newStr1");. This creates 2 string objects. One in regular heap and another in string pool. Why 2 objects and their usage? Why not just one on pool?

  2. If we create object as String str2 = new String("newStr2").intern();. This checks if a similar (meaningfully equal) object is there in pool, give a reference to it. If none, it creates one in pool but not in heap? If so then we should use intern most of the times to save memory? Though it would impact performance a bit. So basically it's String str2 = "newStr2"; (interning is implicit for string literals)

  3. After Java-6 the string pool moved from perm gen space to heap area? So basically we have now just one area as heap or does string pool sits as a separate section in heap now? If it's not a separate section then still 2 objects are created?

Upvotes: 0

Views: 1546

Answers (2)

Adrian Shum
Adrian Shum

Reputation: 40036

Just think the String pool being a special place to store Strings. You can consider String pool is also in the heap (it actually is as far as I can tell), just the pool is handled with care that there will be only one copy of each value in it so the same instance can be reused easily (e.g. when you declare a string literal)

  1. Because it is told to do so. "newStr1" is a String literal and it will be created in /looked up from the pool. String str1 = new String (...) You are telling JVM to create a new instance of String object. So it is simply following what you tell it to do. Of course JIT may optimize it to avoid creating new instance (is it doing such kind of optimization now?) but the behavior you described is simply what JVM told to do.

  2. It just worked same as above. However, after you new String(...) (which created a new String object), you called intern() and hence do the lookup in String pool. Again, the behavior is what it is told to do. And yes, you are right, in your case the result is just the same as String str1="asdf"; with some unnecessary work done. However there are cases that the constructor is not taking a String literal and you still want to enforce the String to be in string pool. In such case intern() will become meaningful.

  3. Perm Gen IS part of the heap area. (Sun/Oracle pre-8) JVM divides the heap into different generations and move objects around, which affect how objects are GC-ed . PermGen is simply the part in heap that never got GC-ed. However, it is still part of heap. Therefore, PermGen is more related to GC algorithm and you may notice, there is no PermGen in JDK8 anymore.

Upvotes: 2

Madhesh
Madhesh

Reputation: 69

In java if JVM identifies the literal("xyz") it will immediately create an object in String constant pool and at the same time if it identifies new then it will create an object in heap.now moving forward to queries:

1.String str1 = new String ("newStr1"); here for JVM "newStr1" is literal so it will follow rules corresponding to literals,so it creates an object in String constant pool. new String("newStr1")-- here for new operator it follows the rules corresponding to new operator,so it will create an object in heap as well.

2.intern():here intern will not create an object in pool.interno is for changing your string object to refer to string pool instead of heap. string s1 = new String("newStr1") -- here s1 will be pointing to heap area not string constant pool.String s1 = new String("newStr1").intern() will make s1 to point to string constant pool,because newStr1 is created in both heap and string constant pool.intern() will not deal with string object creation.

3.permgen : for making effective garbage collection heap is divided into different regions like among that permgen is also a region,still it is inside heap only,it is a region inside heap.please refer this http://www.cubrid.org/blog/dev-platform/understanding-java-garbage-collection/ link for heap regions.

Upvotes: 3

Related Questions