DEV Community

Discussion on: Off-heap memory in Java

Collapse
 
netikras profile image
Darius Juodokas

I agree that it's all pretty and smells nice, but all that is until you have to think about limits.

How much memory will you assign a container, if you are using off-heap?
How will you know which limits were breached and you got the JVM OOMKilled?
Did you spin up one too many threads? Did your off-heap memory store get one byte too large?

This poses a problem, because, while it's easy to estimate and monitor Heap, it's very difficult to monitor off-heap. In fact, if you go ahead and enable NMT (for monitoring or whatever), you will add 2-3 instructions to each malloc, and in the end, your off-heap implementation will be much slower, than the one, where you ask a GC to do the allocations.

The awesomeness of the Heap-full implementation of your (or any other) algorithm is that you can apply limits to it, AND you can monitor it. Meaning, you can
a) predict, when you're about to get an OOME
b) make an educated guess what was the reason for an OOME by analysing retrospective metrics

None of that applies to off-heap. Off-heap is like sailing uncharted waters: you can do anything you like in there, but bear in mind that no laws protect you. Here be dragons!

I strongly believe that playing with off-heap is a VERY dangerous matter and only should be considered when all the other options are exhausted.

Collapse
 
jeissonflorez29 profile image
Jeisson Florez

Hi Darius, thanks for reading and commenting.

It's true that managing memory out of the "standards" that the JVM provides, brings other concerns and problems to think about, however off-heap is not a replacement of the heap memory for all cases and should be something to evaluate carefully depending on the case.

As I mentioned in the post it could be a good try when we are dealing with a lot of data stored in memory, or even when that data exceeds the physical memory and using disk is an option.

Finally, there are many options for addressing design problems and I just wanted to show one that I found interesting IMHO.