NOTE
Off-Heap Memory
1. What off-heap memory is 2. NIO off-heap memory 3. Netty off-heap memory 4. References
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Is Off-Heap Memory?
It means memory that is not allocated on the Java heap, but in operating-system memory.
2. NIO Off-Heap Memory
Memory allocated by JDK NIO’s ByteBuffer.allocate method is on the Java heap. When data needs to be transferred, the data in that heap memory has to be copied unchanged into operating-system memory.
Memory allocated by Java NIO’s ByteBuffer.allocateDirect method is in operating-system memory, so this copy is not needed.
3. Netty Off-Heap Memory
When Netty allocates memory, it decides how much to allocate this time based on the size allocated previously. It stores values such as 16, 32, 48 … 496 and 512, 1024 … in an array. This array represents the amount of memory allocated each time.
At the same time, whether heap or off-heap memory is allocated is determined by whether the Unsafe class is available on the current classpath. If off-heap memory is used, the final call is NIO’s ByteBuffer.allocateDirect; otherwise a byte array is allocated.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub