gpt4 book ai didi

java - 将 double 转换为 String 的最少 GC 消耗方法,无需 E 和 10 位精度

转载 作者:行者123 更新时间:2023-12-01 09:38:29 25 4
gpt4 key购买 nike

我的 Spark 工作是创建很多小对象并遇到垃圾收集问题。我尝试了多种方法将 double 转换为具有 10 位精度的字符串,无需科学记数法。

第一种方法

String.format("%.10f", denomValue)

此案例的错误:

java.lang.OutOfMemoryError: GC overhead limit exceeded
at sun.misc.FormattedFloatingDecimal.create(FormattedFloatingDecimal.java:254)
at sun.misc.FormattedFloatingDecimal.fillDecimal(FormattedFloatingDecimal.java:267)
at sun.misc.FormattedFloatingDecimal.<init>(FormattedFloatingDecimal.java:76)
at sun.misc.FormattedFloatingDecimal.valueOf(FormattedFloatingDecimal.java:38)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:3298)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:3238)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2802)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2753)
at java.util.Formatter.format(Formatter.java:2520)
at java.util.Formatter.format(Formatter.java:2455)
at java.lang.String.format(String.java:2925)

第二种方式

new BigDecimal(d).setScale(10,BigDecimal.ROUND_UP).toPlainString()

这种情况下的错误:-

java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.math.MutableBigInteger.divideMagnitude(MutableBigInteger.java:1489)
at java.math.MutableBigInteger.divideKnuth(MutableBigInteger.java:1227)
at java.math.MutableBigInteger.divide(MutableBigInteger.java:1153)
at java.math.MutableBigInteger.divide(MutableBigInteger.java:1147)
at java.math.BigDecimal.divideAndRound(BigDecimal.java:4326)
at java.math.BigDecimal.setScale(BigDecimal.java:2470)

最佳答案

引用“Understand the OutOfMemoryError Exception”:

Exception in thread thread_name: java.lang.OutOfMemoryError: GC Overhead limit exceeded

Cause: The detail message "GC overhead limit exceeded" indicates that the garbage collector is running all the time and Java program is making very slow progress. After a garbage collection, if the Java process is spending more than approximately 98% of its time doing garbage collection and if it is recovering less than 2% of the heap and has been doing so far the last 5 (compile time constant) consecutive garbage collections, then a java.lang.OutOfMemoryError is thrown. This exception is typically thrown because the amount of live data barely fits into the Java heap having little free space for new allocations.

Action: Increase the heap size. The java.lang.OutOfMemoryError exception for GC Overhead limit exceeded can be turned off with the command line flag -XX:-UseGCOverheadLimit.

如果你的代码确实花费了 98% 的时间进行垃圾回收,那么你绝对应该增加堆大小,因为这意味着 98% 的内存用于(半)永久数据,并且少于 2 % 对于临时分配是免费的。

您可能存在严重的内存泄漏问题。

但正如它所说,如果需要,您可以禁用该特定错误。

对于您的代码,您应该坚持使用选项 1。选项 2 会比选项 1 产生更多的垃圾。

关于java - 将 double 转换为 String 的最少 GC 消耗方法,无需 E 和 10 位精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38640772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com