Title: Java String Pool—How Unbounded intern() Causes PermGen OOM
Open Graph Title: Java String Pool—How Unbounded intern() Causes PermGen OOM
Description: After 3 days, a trading platform hit PermGen OOM from unbounded String.intern() on 500K XML tags. Learn the one JVM flag that can prevent it.
Open Graph Description: After 3 days, a trading platform hit PermGen OOM from unbounded String.intern() on 500K XML tags. Learn the one JVM flag that can prevent it.
Opengraph URL: https://thecodeforge.io/java/string-pool-java/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/java/string-pool-java/"
},
"headline": "Java String Pool—How Unbounded intern() Causes PermGen OOM",
"description": "After 3 days, a trading platform hit PermGen OOM from unbounded String.intern() on 500K XML tags. Learn the one JVM flag that can prevent it.",
"keywords": "Java String Pool—How Unbounded intern() Causes PermGen OOM, Java, Advanced, Strings, After 3 days, a trading platform hit PermGen OOM from unbounded String.intern() on 500K XML tags. Learn the one JVM flag that can prevent it.",
"proficiencyLevel": "Expert",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-05T00:00:00+00:00",
"dateModified": "2026-08-30T00:00:00+00:00",
"url": "https://thecodeforge.io/java/string-pool-java/",
"inLanguage": "en",
"educationalLevel": "Advanced",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Java", "item": "https://thecodeforge.io/java/"},
{"@type": "ListItem", "position": 3, "name": "Java String Pool—How Unbounded intern() Causes PermGen OOM", "item": "https://thecodeforge.io/java/string-pool-java/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "Is the Java String Pool thread-safe?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes, but with nuance. The pool's underlying hash table uses striped locking — each bucket has its own lock rather than one global lock. This means concurrent intern() calls on strings hashing to different buckets can proceed in parallel. However, high throughput on a narrow set of hash buckets can still cause contention. The pool structure itself is safe; the performance characteristics under concurrency require profiling."
}
},
{
"@type": "Question",
"name": "Does String.valueOf() or Integer.toString() put the result in the pool?",
"acceptedAnswer": {
"@type": "Answer",
"text": "No. Methods like String.valueOf(42) and Integer.toString(someNumber) return new heap-allocated String objects, not pooled ones. The only way a runtime-created string ends up in the pool is via explicit intern() or if the JVM's G1 deduplication shares its backing byte array (which doesn't affect == equality anyway). If you need the result pooled, call .intern() on the return value — but only if you have a genuine use case for it."
}
},
{
"@type": "Question",
"name": "Why does == work for string comparison in some situations but not others?",
"acceptedAnswer": {
"@type": "Answer",
"text": "The == operator always compares object references — memory addresses. It happens to return true for string literals and compile-time constant concatenations because those all resolve to the same pooled object. It fails for runtime-created strings (new String(), StringBuilder.toString(), method return values) because those are separate heap objects. The safe rule: always use equals() for string value comparison. Treat any == returning true for strings as an implementation detail, not a contract."
}
},
{
"@type": "Question",
"name": "How can I check the current size of the String Pool in a running JVM?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Use the 'jcmd' command: 'jcmd VM.stringtable'. This prints the number of entries, bucket distribution, and memory usage. Alternatively, you can use '-XX:+PrintStringTableStatistics' at JVM startup to see pool details at exit. For live monitoring, JMX MBeans don't expose the pool directly, but 'jcmd' is the standard tool."
}
},
{
"@type": "Question",
"name": "What happens if I intern() a very long string (e.g., 1 MB)?",
"acceptedAnswer": {
"@type": "Answer",
"text": "The entire string gets added to the pool's hash table. On modern JDKs, this is allowed, but it consumes heap space proportional to the string size. Since the pool never shrinks until a full GC, a very long interned string will stay in heap until no references remain. For large strings, consider using a different caching strategy (e.g., a separate WeakHashMap) rather than interning, because intern() is designed for many small strings, not large blobs."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large