apache/flink · error · NumberFormatException

text does not start with a number

Error message

text does not start with a number

What it means

MemorySize.parseBytes throws NumberFormatException("text does not start with a number") when the trimmed input has no leading ASCII digits. The parser first scans only characters 0-9, so input like "gb", "1gb" is fine but "gb1", "-1mb", or ".5gb" yields an empty numeric part and this exception. Note this is a NumberFormatException even though sibling failures use IllegalArgumentException.

Source

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

        final String trimmed = text.trim();
        if (trimmed.isEmpty()) {
            throw new IllegalArgumentException("argument is an empty- or whitespace-only string");
        }

        final int len = trimmed.length();
        int pos = 0;

        char current;
        while (pos < len && (current = trimmed.charAt(pos)) >= '0' && current <= '9') {
            pos++;
        }

        final String number = trimmed.substring(0, pos);
        final String unit = trimmed.substring(pos).trim().toLowerCase(Locale.US);

        if (number.isEmpty()) {
            throw new NumberFormatException("text does not start with a number");
        }

        final long value;
        try {
            value = Long.parseLong(number); // this throws a NumberFormatException on overflow
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException(
                    "The value '"
                            + number
                            + "' cannot be re represented as 64bit number (numeric overflow).");
        }

        final long multiplier = parseUnit(unit).map(MemoryUnit::getMultiplier).orElse(1L);
        final long result = value * multiplier;

        // check for overflow
        if (result / multiplier != value) {
            throw new IllegalArgumentException(

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Write sizes as an unsigned integer followed by a unit: 512mb, 1g, 1024
  2. For fractional sizes, convert to a smaller unit (1.5gb -> 1536mb)
  3. Strip nothing — leading '+'/'-' signs and decimals are not supported; fix the producer of the string

Example fix

# before
taskmanager.memory.task.heap.size: -1m

# after
taskmanager.memory.task.heap.size: 64m
Defensive patterns

Strategy: validation

Validate before calling

if (!raw.trim().matches("\\d+.*")) {
    throw new IllegalArgumentException("Memory size '" + raw + "' must start with an unsigned integer (e.g. 64m)");
}
long bytes = MemorySize.parseBytes(raw);

Try / catch

catch (NumberFormatException e) { /* note: subclass of IllegalArgumentException; input lacks leading digits — fix the string format */ }

Prevention

When it happens

Trigger: parseBytes("gb"); parseBytes("-1mb") (the '-' stops digit scanning); parseBytes(".5gb") (leading dot is not a digit); values whose unit was concatenated before the number.

Common situations: Negative memory values in config (rejected here because the parser only accepts unsigned integers); decimal values like 1.5gb (only the integer part is scanned — use whole numbers with a smaller unit instead); typos in unit strings.

Related errors


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