0

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.

Magnetron
  • 7,495
  • 1
  • 25
  • 41
NEHA
  • 9
  • 1
  • 1
    Short answer - an `OutOfMemoryError`. You didn't specify whether there's a swarm of compile time string constants or strings are being repeatedly added to the pool using `intern()` at runtime? – Alexander Ivanchenko Aug 01 '22 at 14:27
  • string constants – NEHA Aug 02 '22 at 10:06
  • 3
    Does this answer your question? [What happens if String Pool runs out of memory?](https://stackoverflow.com/questions/30911800/what-happens-if-string-pool-runs-out-of-memory) – P Mittal Aug 04 '22 at 07:38

2 Answers2

1

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.

Holger
  • 285,553
  • 42
  • 434
  • 765
0

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

P Mittal
  • 172
  • 6