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
- Fix the computation so the byte value is >= 0 (commonly increase the total memory config or reduce other components)
- Clamp or validate at the source: reject negative inputs before constructing MemorySize
- If using ofMebiBytes, ensure the mebi-byte argument is non-negative
- 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
- Sanity-check derived memory components (total minus fixed segments) before constructing MemorySize
- Never pass user-supplied negative numbers into MemorySize factories
- Raise taskmanager.memory.process.size when fixed components leave no room for derived ones
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
- argument is an empty- or whitespace-only string
- Segment size must be a power of 2!
- The task heap memory should be positive.
- multiplier must be >= 0
- divisor must be != 0
AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14).
Data as JSON: /api/errors/d8bae2c4eabea621.
Report an issue: GitHub.