java - 我正在寻找在堆内存的 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
解决方案
从 JDK 8u20 切换到 G1GC,它具有字符串重复数据删除功能(请参阅JEP 192:G1 中的字符串重复数据删除)。
为了进一步减少从 JDK 9 开始的内存消耗,ISO-8859-1/Latin-1 字符串可以在内部压缩为字节(请参阅JEP 254:压缩字符串)。
推荐阅读
- html - 背景颜色覆盖引导程序中的文本
- python - Heroku Moduke 未找到错误
- java - K-means 中的总距离总和是否必须始终减少?
- r - 检查日期列表是否在 r 的日期范围内
- php - 使用 Pusher 和 Vue.js 在 laravel 聊天中的 GET 和 POST 问题
- java - 按钮 onClickListener 在 Android 中无法播放声音
- javascript - Spectron 异步 isExisting 不返回 false
- python - 连接 Keras 模型/替换输入但保留层
- xcode - 不支持的会话配置(React Native 和 ARKit)
- firebase - 删除用户经过身份验证的 Firebase 角度 5