Java / Java 17 Garbage Collection Interview Questions
What is string deduplication in G1 GC?
String deduplication, enabled with -XX:+UseStringDeduplication, is a G1-specific optimization that finds String objects with identical backing character arrays and makes them share a single copy of that array in memory.
String objects themselves remain fully immutable and distinct - only the underlying char[]/byte[] storage is shared - so this is safe and transparent to application code. G1 identifies duplicate candidates opportunistically during its concurrent marking phase, using a hash of the array contents, so the cost is spread out rather than paid in a dedicated pause.
It's most effective for applications that intern or otherwise accumulate many logically-identical strings at runtime, such as parsers, JSON/XML processors, or systems repeatedly building similar log or request strings - it can noticeably cut heap footprint in those cases with essentially no functional change to the code.
More Related questions...