MyCATApache/Mycat-Server · error · IllegalArgumentException

Negative size value. Size must be positive:

Error message

Negative size value. Size must be positive: 

What it means

ByteUnit.toBytes() converts a quantity in the given unit to bytes. The library throws this IllegalArgumentException when the input value is negative, because memory sizes must be non-negative; a negative size would corrupt downstream allocation logic.

Solutions

  1. Print and inspect the value passed to toBytes; trace where the negative number originates (usually a subtraction or bad config).
  2. Clamp or validate sizes at the configuration boundary before conversion: if (size < 0) throw or default.
  3. If a negative value is legitimately possible, restructure the computation rather than converting it.

Example fix

// before
long bytes = BYTE.toBytes(used - total);
// after
long delta = used - total;
long bytes = delta < 0 ? 0 : BYTE.toBytes(delta);
Defensive patterns

Strategy: validation

Validate before calling

if (value < 0) throw new IllegalArgumentException("Size must be positive, got: " + value);
long bytes = unit.toBytes(value);

Try / catch

try {
  long bytes = unit.toBytes(value);
} catch (IllegalArgumentException e) {
  // log offending value and fall back to a default size
  bytes = defaultBytes;
}

Prevention

When it happens

Trigger: Calling any ByteUnit conversion helper (e.g. toBytes) with a negative long, typically a size computed from configuration, subtraction (a - b where b > a), or an uninitialized/overflowed variable.

Common situations: Memory config values that are negative due to subtraction of two memory budgets, parsing negative numbers from properties files, or off-by-one arithmetic when computing free memory.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/03d0e2324c5f9494. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/memory/unsafe/utils/ByteUnit.java:55

  // Convert the provided number (d) interpreted as this unit type to unit type (u).
  public long convertTo(long d, ByteUnit u) {
    if (multiplier > u.multiplier) {
      long ratio = multiplier / u.multiplier;
      if (Long.MAX_VALUE / ratio < d) {
        throw new IllegalArgumentException("Conversion of " + d + " exceeds Long.MAX_VALUE in "
          + name() + ". Try a larger unit (e.g. MiB instead of KiB)");
      }
      return d * ratio;
    } else {
      // Perform operations in this order to avoid potential overflow
      // when computing d * multiplier
      return d / (u.multiplier / multiplier);
    }
  }

  public double toBytes(long d) {
    if (d < 0) {
      throw new IllegalArgumentException("Negative size value. Size must be positive: " + d);
    }
    return d * multiplier;
  }

  public long toKiB(long d) { return convertTo(d, KiB); }
  public long toMiB(long d) { return convertTo(d, MiB); }
  public long toGiB(long d) { return convertTo(d, GiB); }
  public long toTiB(long d) { return convertTo(d, TiB); }
  public long toPiB(long d) { return convertTo(d, PiB); }

  private final long multiplier;
}

View on GitHub (pinned to 65f8d8beb7)