java中double强制转换int引发的OOM问题记录
作者:冷雨夜中漫步
这篇文章主要介绍了java中double强制转换int引发的OOM问题记录,本文给大家分享问题排查过程,感兴趣的朋友跟随小编一起看看吧
问题代码
最近出现了一次OOM问题,问题代码如下
public void prcess(double total, int step) { int num = (int) Math.floor(total / step); while (num-- > 0) { doSomething(); // 普通方法,占用内存不大 } }
根据单步步长step将total值划分后,然后进行处理。然后日志中发现堆栈在doSomething()
时报错OOM,抛出java.lang.OutOfMemoryError。确认过doSomething()方法不会占用过大的内存,那为何会发生问题?
排查过程
怀疑num过大导致,查看入参total为合理值,但是step是取配置值,配置未配置时默认为0,则问题代码为int num = (int) Math.floor(total / step);
,这里step为0,Math.floor(total / step) = Infinity,强制转为int后得到值为2147483647。一直在处理此处的逻辑导致内存增长,进而发生OOM
double特殊值与强制转换
java语言中浮点运算存在两个值:NaN与Infinity
NaN
Not-a-Number,非数字值,数学上0/0是无意义的,java在浮点运算中实现了NaN,定义为0.0/0.0。NaN有如下几个特点:
NaN与任何数字都不相等,NaN与其本身也不相等
System.out.println(Double.NaN == Double.NaN); // false System.out.println(Double.NaN == 0.0/0.0); // false System.out.println(Double.NaN == 0.0); // false
使用Double.isNaN或Float.isNaN确认是否为NaN
System.out.println(Double.isNaN(0.0/0.0)); // true System.out.println(Float.isNaN(0.0f/0.0f)); // true
NaN与任何数字比较都是false
System.out.println(Double.NaN > 0.0); // false System.out.println(Double.NaN < 0.0); // false
Infinity
无限,分为正无限与负限小,定义为一个非0数字除以0。
有如下几个特点:
正无限与负无限定义
public static final double POSITIVE_INFINITY = 1.0 / 0.0; public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
无限与任何非无限非0数字运算仍为无限
System.out.println(Double.POSITIVE_INFINITY + Double.MAX_VALUE); // Infinity System.out.println(Double.POSITIVE_INFINITY - Double.MAX_VALUE); // Infinity System.out.println(Double.POSITIVE_INFINITY * Double.MAX_VALUE); // Infinity System.out.println(Double.POSITIVE_INFINITY / Double.MAX_VALUE); // Infinity System.out.println(Double.NEGATIVE_INFINITY + Double.MIN_VALUE); // -Infinity System.out.println(Double.NEGATIVE_INFINITY - Double.MIN_VALUE); // -Infinity System.out.println(Double.NEGATIVE_INFINITY * Double.MIN_VALUE); // -Infinity System.out.println(Double.NEGATIVE_INFINITY / Double.MIN_VALUE); // -Infinity
无限与0运算为无限或NaN
System.out.println(Double.POSITIVE_INFINITY / 0); // Infinity System.out.println(Double.POSITIVE_INFINITY * 0); // NaN System.out.println(Double.NEGATIVE_INFINITY / 0); // -Infinity System.out.println(Double.NEGATIVE_INFINITY * 0); // NaN
负无限与正无限运算为无限或NaN
System.out.println(Double.POSITIVE_INFINITY + Double.NEGATIVE_INFINITY); // NaN System.out.println(Double.POSITIVE_INFINITY - Double.NEGATIVE_INFINITY); // Infinity System.out.println(Double.NEGATIVE_INFINITY + Double.POSITIVE_INFINITY); // NaN System.out.println(Double.NEGATIVE_INFINITY - Double.POSITIVE_INFINITY); // -Infinity System.out.println(Double.POSITIVE_INFINITY * Double.NEGATIVE_INFINITY); // -Infinity System.out.println(Double.POSITIVE_INFINITY / Double.NEGATIVE_INFINITY); // NaN System.out.println(Double.NEGATIVE_INFINITY * Double.POSITIVE_INFINITY); // -Infinity System.out.println(Double.NEGATIVE_INFINITY / Double.POSITIVE_INFINITY); // NaN
无限强制转换为整形会有精度丢失
System.out.println((int)Double.POSITIVE_INFINITY); // Integer.MAX_VALUE System.out.println((int)Double.NEGATIVE_INFINITY); // Integer.MIN_VALUE System.out.println((long) Double.POSITIVE_INFINITY); // Long.MAX_VALUE System.out.println((long)Double.NEGATIVE_INFINITY); // Long.MIN_VALUE
到此这篇关于java中double强制转换int引发的OOM问题的文章就介绍到这了,更多相关java强制转换double转int内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!