apache/flink · error · ArithmeticException

long overflow

Error message

long overflow

What it means

MemorySize.multiply throws ArithmeticException("long overflow") when the BigDecimal product of bytes and multiplier exceeds Long.MAX_VALUE. Flink caps memory sizes at what a signed 64-bit long can represent, so multiplying a large size by a large factor overflows the representable range.

Source

Thrown at flink-core-api/src/main/java/org/apache/flink/configuration/MemorySize.java:215

    // ------------------------------------------------------------------------

    public MemorySize add(MemorySize that) {
        return new MemorySize(Math.addExact(this.bytes, that.bytes));
    }

    public MemorySize subtract(MemorySize that) {
        return new MemorySize(Math.subtractExact(this.bytes, that.bytes));
    }

    public MemorySize multiply(double multiplier) {
        if (multiplier < 0) {
            throw new IllegalArgumentException("multiplier must be >= 0");
        }

        BigDecimal product =
                BigDecimal.valueOf(this.bytes).multiply(BigDecimal.valueOf(multiplier));
        if (product.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) > 0) {
            throw new ArithmeticException("long overflow");
        }
        return new MemorySize(product.longValue());
    }

    public MemorySize divide(long by) {
        if (by < 0) {
            throw new IllegalArgumentException("divisor must be != 0");
        }
        return new MemorySize(bytes / by);
    }

    // ------------------------------------------------------------------------
    //  Parsing
    // ------------------------------------------------------------------------

    /**
     * Parses the given string as as MemorySize.
     *

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Check the multiplier's scale — multipliers are fractions/ratios (e.g. 0.65), not absolute byte counts
  2. Pre-check: `if (BigDecimal.valueOf(multiplier).compareTo(BigDecimal.valueOf((double) Long.MAX_VALUE / Math.max(size.getBytes(), 1))) > 0) ...` before calling
  3. Cap the multiplier or catch ArithmeticException and clamp to Long.MAX_VALUE bytes if that is acceptable

Example fix

// before
MemorySize total = perTask.multiply(numByteUnitsAsMultiplier); // huge factor

// after
MemorySize total = MemorySize.ofMebiBytes(perTask.getMebiBytes() * numUnits);
Defensive patterns

Strategy: validation

Validate before calling

double maxFactor = (double) Long.MAX_VALUE / Math.max(base.getBytes(), 1L);
if (multiplier > maxFactor) {
    throw new IllegalArgumentException("multiplier " + multiplier + " overflows max MemorySize");
}
MemorySize product = base.multiply(multiplier);

Try / catch

catch (ArithmeticException e) { /* overflow means wrong scale — treat as caller bug, fix the factor */ }

Prevention

When it happens

Trigger: size.multiply(Double.MAX_VALUE); multiplying a multi-GB MemorySize by a factor in the billions; multiplier values that came from an inverted or mis-scaled ratio.

Common situations: Unit confusion (passing 1e9 instead of a fraction); ratios computed as total/used with arguments swapped, producing huge multipliers instead of values near 1.0.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/253b2736d9f1afbd. Report an issue: GitHub.