{"record":{"id":"25ef472ad913db68","repo":"apache/flink","slug":"failed-to-serialize-element-serialized-size-n","errorCode":null,"errorMessage":"Failed to serialize element. Serialized size (> {newLen} bytes) exceeds JVM heap space","messagePattern":"Failed to serialize element\\. Serialized size \\(> (.+?) bytes\\) exceeds JVM heap space","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"critical","filePath":"flink-core/src/main/java/org/apache/flink/core/memory/DataOutputSerializer.java","lineNumber":353,"sourceCode":"\n    private void resize(int minCapacityAdd) throws IOException {\n        int newLen = Math.max(this.buffer.length * 2, this.buffer.length + minCapacityAdd);\n        byte[] nb;\n        try {\n            nb = new byte[newLen];\n        } catch (NegativeArraySizeException e) {\n            throw new IOException(\n                    \"Serialization failed because the record length would exceed 2GB (max addressable array size in Java).\");\n        } catch (OutOfMemoryError e) {\n            // this was too large to allocate, try the smaller size (if possible)\n            if (newLen > this.buffer.length + minCapacityAdd) {\n                newLen = this.buffer.length + minCapacityAdd;\n                try {\n                    nb = new byte[newLen];\n                } catch (OutOfMemoryError ee) {\n                    // still not possible. give an informative exception message that reports the\n                    // size\n                    throw new IOException(\n                            \"Failed to serialize element. Serialized size (> \"\n                                    + newLen\n                                    + \" bytes) exceeds JVM heap space\",\n                            ee);\n                }\n            } else {\n                throw new IOException(\n                        \"Failed to serialize element. Serialized size (> \"\n                                + newLen\n                                + \" bytes) exceeds JVM heap space\",\n                        e);\n            }\n        }\n\n        System.arraycopy(this.buffer, 0, nb, 0, this.position);\n        this.buffer = nb;\n        this.wrapper = ByteBuffer.wrap(this.buffer);\n    }","sourceCodeStart":335,"sourceCodeEnd":371,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/core/memory/DataOutputSerializer.java#L335-L371","documentation":"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.","triggerScenarios":"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).","commonSituations":"TaskManager heap too small for the workload; concurrent large serializations; memory leak reducing available heap; oversized single record under heap pressure.","solutions":["Increase TaskManager managed/heap memory (taskmanager.memory.process.size / flink.size).","Reduce concurrent serialization pressure or record sizes; chunk large records.","Profile for memory leaks if heap usage grows over time."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Pre-check available heap before a known-large serialization\nlong free = Runtime.getRuntime().freeMemory();\nif (requiredBytes > free) {\n    throw new IOException(\"Insufficient heap for serialization: need \" + requiredBytes + \", free \" + free);\n}","typeGuard":null,"tryCatchPattern":"try {\n    out.write(payload);\n} catch (IOException e) {\n    if (e.getCause() instanceof OutOfMemoryError) {\n        // heap exhaustion during serialization\n        throw new IOException(\"Serialization exhausted JVM heap; reduce record size or raise task heap\", e);\n    }\n    throw e;\n}","preventionTips":["Size TaskManager heap (taskmanager.memory.flink.size / process.size) for peak serialization load.","Reduce concurrent large serializations and per-record sizes.","Profile heap growth to rule out memory leaks."],"tags":["serialization","memory","out-of-memory","jvm-config"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}