what will happen if our String Memory is full in Java 8 in a real life application

143 Views Asked by At

I am currently working on java 8 Project from last 4 years. In an interview, I was being asked what will happen if your String pool is full. Never encountered it. Already searched a lot didnt find any satisfactory answer for real life app.

2

There are 2 best solutions below

0
P Mittal On

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.
Related Post

0
Holger On

You should ask back what “String pool is full” is supposed to mean. The JVM’s runtime string pool is a hash table of references to objects, the string objects residing in the heap memory like every object.

There are two possibilities what the interviewer could have meant:

  1. The number of strings exceeds the hash table’s size. This is an implementation specific issue. The widespread HotSpot JVM indeed has a fixed size table, however, it can deal with collisions, so only the performance will slightly degrade. This only affects calls of intern() or the first time execution of code containing a string constant, so you might never notice.

  2. There is not enough memory to add a string. This is not different to other scenarios of not having enough memory. E.g. if the allocation of the String fails, it doesn’t matter whether there was the intention of adding it to the pool. The usual response is an OutOfMemoryError.