{"record":{"id":"d8bae2c4eabea621","repo":"apache/flink","slug":"bytes-must-be-0","errorCode":null,"errorMessage":"bytes must be >= 0","messagePattern":"bytes must be >= 0","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"flink-core-api/src/main/java/org/apache/flink/configuration/MemorySize.java","lineNumber":77,"sourceCode":"    // ------------------------------------------------------------------------\n\n    /** The memory size, in bytes. */\n    private final long bytes;\n\n    /** The memorized value returned by toString(). */\n    private transient String stringified;\n\n    /** The memorized value returned by toHumanReadableString(). */\n    private transient String humanReadableStr;\n\n    /**\n     * Constructs a new MemorySize.\n     *\n     * @param bytes The size, in bytes. Must be zero or larger.\n     */\n    public MemorySize(long bytes) {\n        if (bytes < 0) {\n            throw new IllegalArgumentException(\"bytes must be >= 0\");\n        }\n        this.bytes = bytes;\n    }\n\n    public static MemorySize ofMebiBytes(long mebiBytes) {\n        return new MemorySize(mebiBytes << 20);\n    }\n\n    // ------------------------------------------------------------------------\n\n    /** Gets the memory size in bytes. */\n    public long getBytes() {\n        return bytes;\n    }\n\n    /** Gets the memory size in Kibibytes (= 1024 bytes). */\n    public long getKibiBytes() {\n        return bytes >> 10;","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core-api/src/main/java/org/apache/flink/configuration/MemorySize.java#L59-L95","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before\nMemorySize heap = MemorySize.ofMebiBytes(requested - reserved); // can be negative\n\n// after\nlong mb = Math.max(0, requested - reserved);\nMemorySize heap = MemorySize.ofMebiBytes(mb);","handlingStrategy":"validation","validationCode":"long bytes = computeSize();\nif (bytes < 0) {\n    throw new IllegalStateException(\"Computed memory size negative: \" + bytes + \"; check total vs fixed components\");\n}\nMemorySize size = new MemorySize(bytes);","typeGuard":null,"tryCatchPattern":"catch (IllegalArgumentException e) { /* rethrow with the config keys whose combination produced a negative size */ }","preventionTips":["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"],"tags":["memory","configuration","validation","flink-core-api"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}