Reputation: 113
I was analyzing the heap dump of my application and found there are lots of duplicate strings in the heap. I am looking for a way to minimize the memory consumed by the duplicated strings.
Below is the list of the String objects with their count in the heap.
Duplicate String Percentage Wasted Count
"java.lang.Throwable\n at
org.apache.log4j.spi.LoggingEvent.getLocationInformation(LoggingEvent.java:247)\n
at org.apache.log4j ...[length 4460]"
1.25mb 147
"java.lang.Throwable\n at
org.apache.log4j.spi.LoggingEvent.getLocationInformation(LoggingEvent.java:247)\n
at org.apache.log4j ...[length 10742]"
861.96kb 42
"java.lang.Throwable\n at
org.apache.log4j.spi.LoggingEvent.getLocationInformation(LoggingEvent.java:247)\n
at org.apache.log4j ...[length 10744]"
861.96kb 42
"file" 610.99kb 19,495
"java.lang.Throwable\n at
org.apache.log4j.spi.LoggingEvent.getLocationInformation(LoggingEvent.java:247)\n
at org.apache.log4j ...[length 4493]"
608.6kb 70
Upvotes: 2
Views: 174
Reputation: 44980
Switch to G1GC as from JDK 8u20 it has string deduplication feature (see JEP 192: String Deduplication in G1).
To further reduce memory consumption starting from JDK 9 ISO-8859-1/Latin-1 strings can be internally compacted to bytes (see JEP 254: Compact Strings).
Upvotes: 4