# Compact Off-Heap Structures/Tuples In Java

DevFeed: [Compact Off-Heap Structures/Tuples In Java](<https://devfeed.tech/articles/compact-off-heap-structures-tuples-in-java-13628.md>)

Original publisher: [Read original article](<https://mechanical-sympathy.blogspot.com/2012/10/compact-off-heap-structurestuples-in.html>)

Author: Martin Thompson (noreply@blogger.com)

Published: 2012-10-17T12:36:00Z

Content type: article

Language: en

Sources: [Mechanical Sympathy](<https://devfeed.tech/sources/mechanical-sympathy.md>)

Topics: [Java](<https://devfeed.tech/topics/java.md>), [Data structures](<https://devfeed.tech/topics/data-structures.md>), [big-data](<https://devfeed.tech/topics/big-data.md>), [data](<https://devfeed.tech/topics/data.md>)

Tags: [arrays](<https://devfeed.tech/tags/arrays.md>), [big-data](<https://devfeed.tech/tags/big-data.md>), [code](<https://devfeed.tech/tags/code.md>), [garbage-collection](<https://devfeed.tech/tags/garbage-collection.md>), [java](<https://devfeed.tech/tags/java.md>), [latency](<https://devfeed.tech/tags/latency.md>), [memory-management](<https://devfeed.tech/tags/memory-management.md>), [performance](<https://devfeed.tech/tags/performance.md>)

## AI overview

This article explains how to simulate arrays of structures in Java using off-heap memory. It compares direct ByteBuffer with Unsafe, describing trade-offs involving memory layout, size limits, bounds checking, and performance, especially for large data sets and extreme-performance applications.

## Source excerpt

In my last post I detailed the implications of the access patterns your code takes to main memory. Since then I've had a lot of questions about what can be done in Java to enable more predictable memory layout. There are patterns that can be applied using array backed structures which I will discuss in another post. This post will explore how to simulate a feature sorely missing in Java - arrays of structures similar to what C has to offer. Structures are very useful, both on the stack and the heap. To my knowledge it is not possible to simulate this feature on the Java stack. Not being able to do this on the stack is such as shame because it greatly limits the performance of some parallel algorithms, however that is a rant for another day. In Java, all user defined types have to exist on the heap. The Java heap is managed by the garbage collector in the general case, however there is more to the wider heap in a Java process. With the introduction of direct ByteBuffer, memory can be allocated which is not tracked by the garbage collector because it can be available to native code for tasks like avoiding the copying of data to and from the kernel for IO. So one method of managing structures is to fake them within a ByteBuffer as a reasonable approach. This can allow compact data representations, but has performance and size limitations. For example, it is not possible to have a ByteBuffer greater than 2GB, and all access is bounds checked which impacts performance. An alternative exists using Unsafe that is both faster and and not size constrained like ByteBuffer. The approach I'm about to detail is not traditional Java. If your problem space is dealing with big data, or extreme performance, then there are benefits to be had. If your data sets are small, and performance is not an issue, then run away now to avoid getting sucked into the dark arts of native memory management. The benefits of the approach I'm about to detail are: Significantly improved performance More