SoFunction
Updated on 2025-03-03

Record of OOM problems caused by double cast int in java

Problem code

Recently, there was an OOM problem, and the problem code is as follows

public void prcess(double total, int step) {
	int num = (int) (total / step);
	while (num-- > 0) {
		doSomething(); // Normal method, not much memory occupancy	}
}

The total value is divided according to the step-by-step step, and then processed. Then the logs found that the stack isdoSomething()An error OOM is reported and thrown. It has been confirmed that the doSomething() method will not occupy too much memory, so why does the problem occur?

Troubleshooting process

It is suspected that num is too large, so the view parameter total is a reasonable value, but step is to get the configuration value. If the configuration is not configured, the default is 0, so the problem code isint num = (int) (total / step);, Here step is 0, (total/step) = Infinity, after forcing it to int, the value is 2147483647. The logic here has been dealing with, causing memory to grow, and then OOM occurs

Double special value and cast

There are two values ​​for floating point operations in Java language: NaN and Infinity

NaN

Not-a-Number, non-numeric value, mathematically 0/0 is meaningless, Java implements NaN in floating point operation, defined as 0.0/0.0. NaN has the following characteristics:

NaN is not equal to any number, and NaN is not equal to itself.

( == ); // false
( == 0.0/0.0); // false
( == 0.0); // false

Use or confirm whether it is NaN

((0.0/0.0));  // true
((0.0f/0.0f));  // true

NaN is false compared to any number

( > 0.0); // false
( < 0.0); // false

Infinity

Infinite, divided into positive infinite and negative limit small, defined as a non-0 number divided by 0.

It has the following characteristics:

Positive and negative infinite definitions

public static final double POSITIVE_INFINITY = 1.0 / 0.0;
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;

Infinite and any non-infinite non-0 digit operation is still infinite

(Double.POSITIVE_INFINITY + Double.MAX_VALUE); // Infinity
(Double.POSITIVE_INFINITY - Double.MAX_VALUE); // Infinity
(Double.POSITIVE_INFINITY * Double.MAX_VALUE); // Infinity
(Double.POSITIVE_INFINITY / Double.MAX_VALUE); // Infinity
(Double.NEGATIVE_INFINITY + Double.MIN_VALUE); // -Infinity
(Double.NEGATIVE_INFINITY - Double.MIN_VALUE); // -Infinity
(Double.NEGATIVE_INFINITY * Double.MIN_VALUE); // -Infinity
(Double.NEGATIVE_INFINITY / Double.MIN_VALUE); // -Infinity

Infinite and 0 are calculated as Infinite or NaN

(Double.POSITIVE_INFINITY / 0); // Infinity
(Double.POSITIVE_INFINITY * 0); // NaN
(Double.NEGATIVE_INFINITY / 0); // -Infinity
(Double.NEGATIVE_INFINITY * 0); // NaN

Negative Infinite and Positive Infinite operations are infinite or NaN

(Double.POSITIVE_INFINITY + Double.NEGATIVE_INFINITY); // NaN
(Double.POSITIVE_INFINITY - Double.NEGATIVE_INFINITY); // Infinity
(Double.NEGATIVE_INFINITY + Double.POSITIVE_INFINITY); // NaN
(Double.NEGATIVE_INFINITY - Double.POSITIVE_INFINITY); // -Infinity
(Double.POSITIVE_INFINITY * Double.NEGATIVE_INFINITY); // -Infinity
(Double.POSITIVE_INFINITY / Double.NEGATIVE_INFINITY); // NaN
(Double.NEGATIVE_INFINITY * Double.POSITIVE_INFINITY); // -Infinity
(Double.NEGATIVE_INFINITY / Double.POSITIVE_INFINITY); // NaN

Infinite casting to plastic will result in loss of accuracy

((int)Double.POSITIVE_INFINITY); // Integer.MAX_VALUE
((int)Double.NEGATIVE_INFINITY); // Integer.MIN_VALUE
((long) Double.POSITIVE_INFINITY); // Long.MAX_VALUE
((long)Double.NEGATIVE_INFINITY); // Long.MIN_VALUE

This is the end of this article about the OOM problem caused by double cast int in java. For more related content related to double cast int in java, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!