{"record":{"id":"f0cd3321520b1dc5","repo":"apache/flink","slug":"memory-segment-does-not-represent-heap-memory","errorCode":null,"errorMessage":"Memory segment does not represent heap memory","messagePattern":"Memory segment does not represent heap memory","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java","lineNumber":278,"sourceCode":"     *\n     * @return <tt>true</tt>, if the memory segment is backed by off-heap memory, <tt>false</tt> if\n     *     it is backed by heap memory.\n     */\n    public boolean isOffHeap() {\n        return heapMemory == null;\n    }\n\n    /**\n     * Returns the byte array of on-heap memory segments.\n     *\n     * @return underlying byte array\n     * @throws IllegalStateException if the memory segment does not represent on-heap memory\n     */\n    public byte[] getArray() {\n        if (heapMemory != null) {\n            return heapMemory;\n        } else {\n            throw new IllegalStateException(\"Memory segment does not represent heap memory\");\n        }\n    }\n\n    /**\n     * Returns the off-heap buffer of memory segments.\n     *\n     * @return underlying off-heap buffer\n     * @throws IllegalStateException if the memory segment does not represent off-heap buffer\n     */\n    public ByteBuffer getOffHeapBuffer() {\n        if (offHeapBuffer != null) {\n            return offHeapBuffer;\n        } else {\n            throw new IllegalStateException(\"Memory segment does not represent off-heap buffer\");\n        }\n    }\n\n    /**","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java#L260-L296","documentation":"MemorySegment.getArray() returns the backing byte[] only for on-heap segments (created from a heap array). Off-heap segments have heapMemory == null, so getArray() throws IllegalStateException('Memory segment does not represent heap memory'). Off-heap memory cannot be exposed as a Java array because it lives outside the heap.","triggerScenarios":"Calling getArray() on a segment produced by MemorySegmentFactory.allocateOffHeapMemory / wrapOffHeapBuffer, or on segments handed out by a MemoryManager configured for off-heap memory.","commonSituations":"Code written and tested against heap segments (task-manager memory configured as managed heap or on-heap state backend) then run with off-heap memory configured (taskmanager.memory.managed.size with off-heap consumers, off-heap buffers); serializers or output paths that try to hand the raw byte[] to java.io APIs.","solutions":["Branch on segment.isOffHeap(): use getOffHeapBuffer() (a ByteBuffer view) for off-heap segments and getArray() only for heap segments.","If a contiguous copy is required regardless of backing, copy out the bytes with segment.get(offset, target, 0, len) or wrap via segment.wrap(0, size) instead of assuming an array.","If your component fundamentally requires heap arrays, allocate the segment with MemorySegmentFactory.allocateUnpooledSegment(size) or wrap an existing byte[]."],"exampleFix":"// before\nbyte[] bytes = segment.getArray(); // IllegalStateException on off-heap\n\n// after\nif (segment.isOffHeap()) {\n    ByteBuffer bytes = segment.getOffHeapBuffer();\n    // ... read via ByteBuffer API\n} else {\n    byte[] bytes = segment.getArray();\n}","handlingStrategy":"validation","validationCode":"if (!segment.isOffHeap()) {\n    byte[] bytes = segment.getArray();\n} else {\n    ByteBuffer bytes = segment.getOffHeapBuffer();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never assume a segment is heap-backed; always branch on isOffHeap().","Prefer backing-agnostic APIs (get/put bulk methods, wrap) when array access is not required.","Test utility code with both heap and off-heap segment factories."],"tags":["memory","off-heap","flink-core","api-misuse"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}