Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if String Pool runs out of memory?

What will happen if there are many String literals on String Pool and it runs out of memory. Does it grow its size, if yes how? If not, what will happen if i try to create more String literals?

like image 305
Milan Pandey Avatar asked Jun 18 '15 09:06

Milan Pandey


People also ask

What is string pool memory?

String pool is a storage space in the Java heap memory where string literals are stored. It is also known as String Constant Pool or String Intern Pool. It is privately maintained by the Java String class.

Can we clear string pool memory by programming?

No, typically you can not "destroy reference from String pool in Java" manually. The main reason I suppose why you are targeting it is to avoid out of memory errors. 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.

Does string pool makes Java more memory efficient?

That's why the String pool is moved to a larger heap area. To make the java more memory efficient the concept of string literal is used. By the use of 'new' keyword, the JVM will create a new string object in the normal heap area even if the same string object present in the string pool.

What happens in string memory?

Whenever you create a string object using string literal, that object is stored in the string constant pool and whenever you create a string object using new keyword, such object is stored in the heap memory. For example, when you create string objects like below, they will be stored in the String Constant Pool.


1 Answers

First point first - STRING POOL Doesn't have String Literals

String Pool is a Collection of references that points to the String Objects.

When you write String = "hello" it creates that an String Object "hello" on the heap and will place an reference to this object in the String Literal Pool ( provided no Object is already there on the heap named "Hello")

Point to Note "hello" is added to the constant pool of the corresponding class. Therefore, it can be garbage collected only after the class is unloaded. So when the class is unloaded that Objects gets GC

What will happens?

String pooling is done through a process called string canonicalisation Which is a weakHashMap.This weakHashMap automatically clears out mapping when there is no other references to the keys or values. .ie the string will be garbage collected from the JVM.

Does it Grow in size?

NO STRING POOL DOESNOT GROW IN SIZE- It's is Compile Time Constant

How it Grow in Size ?

You need to specify -XX:StringTableSize=N, at the compile time where N is the string pool map size

At and at Last your question :

What happens if String Pool runs out of memory?

Simplest Answer : You get java.lang.OutOfMemoryError:java.lang.OutOfMemoryError: Java heap space from java 7 onwards . While java.lang.OutOfMemoryError: PermGen space in older version of java like 6

like image 195
Ankur Anand Avatar answered Oct 07 '22 14:10

Ankur Anand