StringBuilder 导致堆内存溢出
StringBuilder 导致堆内存溢出
原始问题描述:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3332)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448)
at java.lang.StringBuilder.append(StringBuilder.java:136)
at testpkg.Main.decDfs(Main.java:230)
定位到问题点:
/**
* For positive values of {@code minimumCapacity}, this method
* behaves like {@code ensureCapacity}, however it is never
* synchronized.
* If {@code minimumCapacity} is non positive due to numeric
* overflow, this method throws {@code OutOfMemoryError}.
*/
private void ensureCapacityInternal(int minimumCapacity) {
// overflow-conscious code
if (minimumCapacity - value.length > 0) {
value = Arrays.copyOf(value,
newCapacity(minimumCapacity));
}
}
问题的原因在于无脑 append 的时候,扩充内存使得 StringBuilder 的长度超过了上限触发OOM,查看后发现 StringBuilder 实际能使用的大小和 JVM 。
解决方法:
- 调大JVM参数,因为StringBuilder有时候没有到达上限,由于JVM堆空间太小,也会触发OOM
- 重新计算最大所需空间,增加 StringBuilder 数量,提前分散放置字符串
- 提前序列化部分结果,但是效率很低