Under what conditions is Off-Heap memory worth it?

What I've read about the off-heap memory interfaces like ByteBuffer and the FFMI make it sound like it's much harder to access than heap memory and incurs a significant performance penalty. But they also neglect to mention the cases where the penalty is mitigated or the trade-off is worth it. So I wanted to ask: under what circumstances is moving something off the heap beneficial?
- Does it specifically have to be something that's rarely accessed, or can it be relatively often? - Does the type of data matter, or can you store most objects there without issue? Which types are better? - What is the performance penalty from storing objects off the heap? Is it just "if it will never be GC'd, you can put it off heap", or is it only useful in certain situations? Basically, what types of memory are good candidates for being moved off the heap?
27 Replies
JavaBot
JavaBot9mo ago
This post has been reserved for your question.
Hey @Haiku! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
nikcho-kouhai
nikcho-kouhai9mo ago
Do correct me if I'm wrong but I don't think there is a way to allocate something that isn't on the heap (excluding primitives) in java There are probably ways to do that through the JNI, but in pure java I don't think it's possible. and generally speaking accessing objects from the heap and accessing objects from other memory (which would usually be the stack) isn't really all that different from a performance standpoint
Haiku
HaikuOP9mo ago
The NIO package has a number of ways to move something off the heap, such as ByteBuffers and memory mapped files.
nikcho-kouhai
nikcho-kouhai9mo ago
well I would assume that the ByteBuffer is also allocated on the heap perhaps it just makes it more convenient for interfacing with aforementioned JNI here is from the official documentation of ByteBuffer:
A byte buffer is either direct or non-direct. Given a direct byte buffer, the Java virtual machine will make a best effort to perform native I/O operations directly upon it. That is, it will attempt to avoid copying the buffer's content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system's native I/O operations.
A byte buffer is either direct or non-direct. Given a direct byte buffer, the Java virtual machine will make a best effort to perform native I/O operations directly upon it. That is, it will attempt to avoid copying the buffer's content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system's native I/O operations.
Haiku
HaikuOP9mo ago
The object that handles memory interfacing is on the heap yes, but it maps to off heap memory. If you don't know that's fine but I really want the input of someone who's used it before
tjoener
tjoener9mo ago
Use ZGC or shenandoah Slightly lower perf, but pauses dissapear
JavaBot
JavaBot9mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
dan1st
dan1st9mo ago
off-heap memory is often worth it when dealing with native memory or mapping files
nikcho-kouhai
nikcho-kouhai9mo ago
I am wondering if there are any actual advantages to this performance wise since any memory access excluding the actual system calls that are required to allocate stuff is just about the same speed. Memory mapping to files would be plenty slower considering how much slower disks are. Of course the operating system does do virtual memory mapping, but it knows when to.
dan1st
dan1st9mo ago
I meant memory-mapping files there can be performance improvements with large chunks of memory
nikcho-kouhai
nikcho-kouhai9mo ago
ah. Well yeah, perhaps, but I doubt it's worth the hassle.
dan1st
dan1st9mo ago
in most cases, no but there are some things where it makes a difference
nikcho-kouhai
nikcho-kouhai9mo ago
can you give an example? I am actually pretty curious
dan1st
dan1st9mo ago
Netty Netty uses off-heap memory for some stuff
nikcho-kouhai
nikcho-kouhai9mo ago
cool, maybe I'll look into it when I have some free time
dan1st
dan1st9mo ago
or it can be useful for networking stuff in general or things where the stuff is accessed by native code
nikcho-kouhai
nikcho-kouhai9mo ago
where would the memory be allocated in that case though? I am using the JNI at the moment and in most cases things are still just on the heap
dan1st
dan1st9mo ago
it just asks the OS for new chunks of memory so it's in the process heap but not the java heap
nikcho-kouhai
nikcho-kouhai9mo ago
well yeah but it's still in the heap
dan1st
dan1st9mo ago
off-heap means outside the Java heap
nikcho-kouhai
nikcho-kouhai9mo ago
ah I though we were talking about off-heap like off-off heap well yeah if you allocate an object natively and interface with jni that object isn't actually managed by gc and it's not on the java heap
dan1st
dan1st9mo ago
well where could you store that? On disk? In the heap of another application?
nikcho-kouhai
nikcho-kouhai9mo ago
that's exactly what I was asking
dan1st
dan1st9mo ago
In Java, heap means the Java heap whose size can be configured with -Xmx
nikcho-kouhai
nikcho-kouhai9mo ago
yeah. I misunderstood , that's my bad
dan1st
dan1st9mo ago
so with off-heap memory, you are not limited to the size configured with -Xmx
JavaBot
JavaBot9mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server