Java-off-heap memory

Java NIO study notes four (detailed explanation of zero copy)

NIO DirectByteBuffer

Java NIO introduced ByteBuffer for the buffer of the channel. ByteBuffer has three main implementations:

HeapByteBuffer

Used when calling ByteBuffer.allocate(). It is called a heap because it is stored in the heap space of the JVM, so you can get all the advantages such as GC support and cache optimization. However, it is not page-aligned, which means that if you need to talk to native code via JNI, the JVM will have to copy to the aligned buffer space.

DirectByteBuffer

Used when calling ByteBuffer.allocateDirect(). JVM will use malloc() to allocate memory space outside of the heap space. Because it is not managed by the JVM, your memory space is page-aligned and not affected by GC, which makes it a perfect choice for handling native code. However, if you want a C programmer to manage this memory yourself, you must allocate and release the memory yourself to prevent memory leaks.

MappedByteBuffer

Used when calling FileChannel.map(). Similar to DirectByteBuffer, this is also the case outside the JVM heap. It is basically used as a wrapper function for OS mmap() system calls, so that the code can directly manipulate the mapped physical memory data

Guess you like

Origin blog.csdn.net/lewee0215/article/details/111828923