apache/flink · critical · IOException

Failed to serialize element. Serialized size (> {newLen} byt

Error message

Failed to serialize element. Serialized size (> {newLen} bytes) exceeds JVM heap space

What it means

Thrown by DataOutputSerializer.resize(int) on the retry path: the doubled allocation failed with OutOfMemoryError, so it retries with the minimal size (buffer.length + minCapacityAdd); that minimal allocation also OOMs, so an IOException reports the failed size and chains the OutOfMemoryError as cause.

Source

Thrown at flink-core/src/main/java/org/apache/flink/core/memory/DataOutputSerializer.java:353

    private void resize(int minCapacityAdd) throws IOException {
        int newLen = Math.max(this.buffer.length * 2, this.buffer.length + minCapacityAdd);
        byte[] nb;
        try {
            nb = new byte[newLen];
        } catch (NegativeArraySizeException e) {
            throw new IOException(
                    "Serialization failed because the record length would exceed 2GB (max addressable array size in Java).");
        } catch (OutOfMemoryError e) {
            // this was too large to allocate, try the smaller size (if possible)
            if (newLen > this.buffer.length + minCapacityAdd) {
                newLen = this.buffer.length + minCapacityAdd;
                try {
                    nb = new byte[newLen];
                } catch (OutOfMemoryError ee) {
                    // still not possible. give an informative exception message that reports the
                    // size
                    throw new IOException(
                            "Failed to serialize element. Serialized size (> "
                                    + newLen
                                    + " bytes) exceeds JVM heap space",
                            ee);
                }
            } else {
                throw new IOException(
                        "Failed to serialize element. Serialized size (> "
                                + newLen
                                + " bytes) exceeds JVM heap space",
                        e);
            }
        }

        System.arraycopy(this.buffer, 0, nb, 0, this.position);
        this.buffer = nb;
        this.wrapper = ByteBuffer.wrap(this.buffer);
    }

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Increase TaskManager managed/heap memory (taskmanager.memory.process.size / flink.size).
  2. Reduce concurrent serialization pressure or record sizes; chunk large records.
  3. Profile for memory leaks if heap usage grows over time.
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-check available heap before a known-large serialization
long free = Runtime.getRuntime().freeMemory();
if (requiredBytes > free) {
    throw new IOException("Insufficient heap for serialization: need " + requiredBytes + ", free " + free);
}

Try / catch

try {
    out.write(payload);
} catch (IOException e) {
    if (e.getCause() instanceof OutOfMemoryError) {
        // heap exhaustion during serialization
        throw new IOException("Serialization exhausted JVM heap; reduce record size or raise task heap", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Serializing a record large enough that even the minimal required buffer cannot be allocated due to JVM heap exhaustion (the doubled-size attempt failed, then the exact-fit retry also failed).

Common situations: TaskManager heap too small for the workload; concurrent large serializations; memory leak reducing available heap; oversized single record under heap pressure.

Related errors


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