首页 > 解决方案 > 我正在寻找在堆内存的 Java 应用程序中处理重复字符串的最佳方法?

问题描述

我正在分析我的应用程序的堆转储,发现堆中有很多重复的字符串。我正在寻找一种方法来最小化重复字符串消耗的内存。

下面是 String 对象的列表及其在堆中的计数。

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

标签: java

解决方案


从 JDK 8u20 切换到 G1GC,它具有字符串重复数据删除功能(请参阅JEP 192:G1 中的字符串重复数据删除)。

为了进一步减少从 JDK 9 开始的内存消耗,ISO-8859-1/Latin-1 字符串可以在内部压缩为字节(请参阅JEP 254:压缩字符串)。


推荐阅读