apache/flink · error · IllegalArgumentException

The task heap memory should be positive.

Error message

The task heap memory should be positive.

What it means

SlotSharingGroup.Builder.setTaskHeapMemory(MemorySize) requires the task heap to be strictly greater than MemorySize.ZERO. Memory of zero or negative bytes is rejected because a group declaring resources must declare a usable heap, matching the requirement that cpu cores and task heap are the two mandatory resources.

Source

Thrown at flink-core-api/src/main/java/org/apache/flink/api/common/SlotSharingGroup.java:164

        private final Map<String, Double> externalResources = new HashMap<>();

        private Builder(String name) {
            this.name = name;
        }

        /** Set the CPU cores for this SlotSharingGroup. */
        public Builder setCpuCores(double cpuCores) {
            if (cpuCores <= 0) {
                throw new IllegalArgumentException("The cpu cores should be positive.");
            }
            this.cpuCores = cpuCores;
            return this;
        }

        /** Set the task heap memory for this SlotSharingGroup. */
        public Builder setTaskHeapMemory(MemorySize taskHeapMemory) {
            if (taskHeapMemory.compareTo(MemorySize.ZERO) <= 0) {
                throw new IllegalArgumentException("The task heap memory should be positive.");
            }
            this.taskHeapMemory = taskHeapMemory;
            return this;
        }

        /** Set the task heap memory for this SlotSharingGroup in MB. */
        public Builder setTaskHeapMemoryMB(int taskHeapMemoryMB) {
            if (taskHeapMemoryMB <= 0) {
                throw new IllegalArgumentException("The task heap memory should be positive.");
            }
            this.taskHeapMemory = MemorySize.ofMebiBytes(taskHeapMemoryMB);
            return this;
        }

        /** Set the task off-heap memory for this SlotSharingGroup. */
        public Builder setTaskOffHeapMemory(MemorySize taskOffHeapMemory) {
            this.taskOffHeapMemory = taskOffHeapMemory;
            return this;

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Provide a positive heap size: setTaskHeapMemory(MemorySize.ofMebiBytes(256)).
  2. Fix sign errors in memory strings and ensure subtraction-based sizing keeps a positive remainder.
  3. Use a non-zero sentinel or Optional<MemorySize> for 'unset' instead of MemorySize.ZERO.

Example fix

# before
task.heap.size=-256m

# after
task.heap.size=256m
Defensive patterns

Strategy: validation

Validate before calling

if (heap == null || heap.compareTo(MemorySize.ZERO) <= 0) throw new IllegalArgumentException("task heap must be positive");
builder.setTaskHeapMemory(heap);

Prevention

When it happens

Trigger: Calling setTaskHeapMemory(MemorySize.ZERO), a negative MemorySize (MEMORY_SIZE parses negative byte counts like "-1m"), or a computed size that evaluates to 0 bytes.

Common situations: Parsing memory strings with a sign error ("-256m"), subtracting managed/off-heap from a total and hitting zero, defaulting to MemorySize.ZERO as 'unset' in config plumbing.

Related errors


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