Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String pool is created in PermGen area or Object area of Heap

HERE, author is saying that

3) String pool is created in PermGen area of Heap, garbage collection can occur in perm space but depends upon JVM to JVM. By the way from JDK 1.7 update, String pool is moved to heap area where objects are created.

Is there any specific reason why is it done ? I am not able to find any online. And what are the implications ?

like image 908
Andy897 Avatar asked Jan 23 '15 10:01

Andy897


People also ask

Is String pool part of heap?

As the name suggests, String Pool in java is a pool of Strings stored in Java Heap Memory. We know that String is a special class in java and we can create String objects using a new operator as well as providing values in double-quotes.

Why did the String pool move from PermGen to the normal heap area?

Why did the String pool move from PermGen to normal heap area? PermGen space is limited space, the default size is just 64 MB. And it was a problem of creating and storing too many string objects in PermGen space. That's why the String pool is moved to a larger heap area.

Is String pool part of Metaspace?

the per-class constant pool is in Metaspace since JDK 8.

When we create a String object it is created in heap or stack?

The stack will store the value of the int literal and references of String and Demo objects. The value of any object will be stored in the heap, and all the String literals go in the pool inside the heap: The variables created on the stack are deallocated as soon as the thread completes execution.


2 Answers

The move to Metaspace was necessary since the PermGen was really hard to tune.

Also, it was difficult to size the PermGen since the size depended on a lot of factors such as the total number of classes, the size of the constant pools, size of methods, etc.

Additionally, each garbage collector in HotSpot needed specialized code for dealing with metadata in the PermGen. Detaching metadata from PermGen not only allows the seamless management of Metaspace, but also allows for improvements such as simplification of full garbage collections and future concurrent de-allocation of class metadata.

like image 124
Walt Avatar answered Sep 24 '22 23:09

Walt


In Java 6 days all interned strings were stored in the PermGen – the fixed size part of heap mainly used for storing loaded classes and string pool. Besides explicitly interned strings, PermGen string pool also contained all literal strings earlier used in your program.

The biggest issue with string pool in Java 6 was its location – the PermGen. PermGen has a fixed size and can not be expanded at runtime. You can set it using -XX:MaxPermSize=N option.

engineers made an extremely important change to the string pooling logic in Java 7 – the string pool was relocated to the heap. It means that you are no longer limited by a separate fixed size memory area. All strings are now located in the heap, as most of other ordinary objects, which allows you to manage only the heap size while tuning your application.

Ref : http://java-performance.info/string-intern-in-java-6-7-8/

like image 23
Tanmoy Bhattacharjee Avatar answered Sep 22 '22 23:09

Tanmoy Bhattacharjee