{"record":{"id":"67023c95cf2d43bb","repo":"apache/flink","slug":"memory-segment-does-not-represent-off-heap-buffer","errorCode":null,"errorMessage":"Memory segment does not represent off-heap buffer","messagePattern":"Memory segment does not represent off-heap buffer","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java","lineNumber":292,"sourceCode":"    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    /**\n     * Returns the memory address of off-heap memory segments.\n     *\n     * @return absolute memory address outside the heap\n     * @throws IllegalStateException if the memory segment does not represent off-heap memory\n     */\n    public long getAddress() {\n        if (heapMemory == null) {\n            return address;\n        } else {\n            throw new IllegalStateException(\"Memory segment does not represent off heap memory\");\n        }\n    }\n\n    /**","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/core/memory/MemorySegment.java#L274-L310","documentation":"MemorySegment.getOffHeapBuffer() returns the underlying ByteBuffer only for off-heap segments. For on-heap segments offHeapBuffer is null, so it throws IllegalStateException('Memory segment does not represent off-heap buffer'). It is the dual of getArray(), which only works for heap segments.","triggerScenarios":"Calling getOffHeapBuffer() on a segment created by MemorySegmentFactory.wrap(byte[]) or allocateUnpooledSegment (heap-backed).","commonSituations":"Utility code assuming all segments are off-heap (e.g. written against a RocksDB/off-heap pipeline) executed against heap segments in tests or on a differently configured cluster; mixing segment factories across code paths.","solutions":["Branch on segment.isOffHeap() before choosing getOffHeapBuffer() vs getArray().","If you only need to read/write bytes positionally, use segment.get(index)/put(index, b) or the bulk get/put methods that work on either backing.","Use segment.wrap(offset, length) to obtain a ByteBuffer view valid for both heap and off-heap owned segments."],"exampleFix":"// before\nByteBuffer buf = segment.getOffHeapBuffer(); // IllegalStateException on heap segment\n\n// after\nByteBuffer buf = segment.isOffHeap()\n        ? segment.getOffHeapBuffer()\n        : segment.wrap(0, segment.size());","handlingStrategy":"validation","validationCode":"if (segment.isOffHeap()) {\n    ByteBuffer buf = segment.getOffHeapBuffer();\n} else {\n    byte[] bytes = segment.getArray();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["getArray() and getOffHeapBuffer() are exact duals — call each only after checking isOffHeap().","Use segment.get(index)/put(index, b) for positional access that works on either backing."],"tags":["memory","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"}