apache/flink · error · IllegalArgumentException

bytes must be >= 0

Error message

bytes must be >= 0

What it means

MemorySize's constructor throws IllegalArgumentException("bytes must be >= 0") when given a negative byte count. MemorySize models a non-negative quantity of memory, so negative sizes are rejected at construction; every factory (ofMebiBytes, add, subtract results) funnels through this check. ofMebiBytes(n) with negative n also lands here because it shifts a negative value.

Source

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

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

    /** The memory size, in bytes. */
    private final long bytes;

    /** The memorized value returned by toString(). */
    private transient String stringified;

    /** The memorized value returned by toHumanReadableString(). */
    private transient String humanReadableStr;

    /**
     * Constructs a new MemorySize.
     *
     * @param bytes The size, in bytes. Must be zero or larger.
     */
    public MemorySize(long bytes) {
        if (bytes < 0) {
            throw new IllegalArgumentException("bytes must be >= 0");
        }
        this.bytes = bytes;
    }

    public static MemorySize ofMebiBytes(long mebiBytes) {
        return new MemorySize(mebiBytes << 20);
    }

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

    /** Gets the memory size in bytes. */
    public long getBytes() {
        return bytes;
    }

    /** Gets the memory size in Kibibytes (= 1024 bytes). */
    public long getKibiBytes() {
        return bytes >> 10;

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Fix the computation so the byte value is >= 0 (commonly increase the total memory config or reduce other components)
  2. Clamp or validate at the source: reject negative inputs before constructing MemorySize
  3. If using ofMebiBytes, ensure the mebi-byte argument is non-negative
  4. Inspect preceding Math.subtractExact/add results — a negative there becomes this exception

Example fix

// before
MemorySize heap = MemorySize.ofMebiBytes(requested - reserved); // can be negative

// after
long mb = Math.max(0, requested - reserved);
MemorySize heap = MemorySize.ofMebiBytes(mb);
Defensive patterns

Strategy: validation

Validate before calling

long bytes = computeSize();
if (bytes < 0) {
    throw new IllegalStateException("Computed memory size negative: " + bytes + "; check total vs fixed components");
}
MemorySize size = new MemorySize(bytes);

Try / catch

catch (IllegalArgumentException e) { /* rethrow with the config keys whose combination produced a negative size */ }

Prevention

When it happens

Trigger: new MemorySize(-1024); MemorySize.ofMebiBytes(-1); a computed size such as a subtraction or a fraction times a total that evaluates negative (e.g. total memory so small that a fixed component exceeds it).

Common situations: Memory configuration arithmetic in Flink memory fraction/derivation code (taskmanager.memory.* derivation) where derived segments go negative on tiny process memory settings; user code computing quotas or limits that can dip below zero.

Related errors


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