Difference Betwixt Direct, Not Straight Together With Mapped Bytebuffer Inwards Java

Advertisement

Masukkan script iklan 970x90px

Difference Betwixt Direct, Not Straight Together With Mapped Bytebuffer Inwards Java

Kamis, 28 Januari 2021

ByteBuffer is ane of the of import shape of Java NIO API. It was introduced inward java.nio bundle on JDK 1.4, it non entirely allows y'all to operate on heap byte arrays but too alongside conduct memory, which resides exterior the JVM. There are mainly iii types f ByteBuffer, Direct, Non-Direct too mapped byte buffers. You tin create both conduct too non-direct buffers using java.nio.ByteBuffer class, field MappedByteBuffer is a subclass of ByteBuffer, which is created yesteryear FileChannel.map() method, to operate on memory mapped file. The primary divergence betwixt conduct too non-direct byte buffers are at that topographic point retention location, non-direct byte buffers are exactly a wrapper unopen to byte array too they reside inward Java Heap retention field conduct byte buffer is exterior of JVM too retention is non allocated from the heap.

You tin too squall back this fact yesteryear their name, Direct indicates working alongside retention directly. Due to this reason, conduct byte buffers are too non affected yesteryear Garbage Collection. MappedByteBuffer is too a type of conduct byte buffer, which correspond a memory-mapped part of a file.

In this Java NIO tutorial, y'all volition run across a couplet of to a greater extent than differences betwixt direct, non-direct too mapped byte buffers, which volition assist y'all to empathize the concept too their usage better.

If y'all dearest books similar me too wants to acquire advanced concept e.g. high performance too depression latency application development, performance tuning, too JVM internals, I advise taking a hold off at the Definitive guide to Java Performance, ane of the must-read books for Java programmers.




Direct vs Non-direct vs MappedByteBuffer inward Java

As I said ByteBuffer is ane of the really of import shape inward a high-performance application. It is widely used inward the high-frequency trading application, which strives for really depression latency, to a greater extent than ofttimes than non inward sub-microsecond level. When I start mentioned close retention mapped file inward Java, I convey outlined some benefits of using those files, too ByteBuffer shape is key to operate them. Most of the differences betwixt conduct too non-direct ByteBuffer derived from the fact that ane is inside heap memory field other is exterior heap.



1) The start divergence betwixt non-direct too conduct byte buffer comes from the fact, how y'all create them. You tin create non-direct byte buffer either yesteryear allocating infinite for buffer's content or yesteryear wrapping an existing byte array into a buffer. While a Direct byte buffer may endure created yesteryear calling manufacturing industrial plant life method allocateDirect() or yesteryear mapping a part of a file straight into retention , known  as MappedByteBuffer.


2) In the illustration of Direct byte buffer, JVM performs native IO operation straight into the buffer, without copying them into whatsoever intermediate buffer, this makes it really attractive for performing high-speed IO functioning on them, but this facility comes alongside care. If a retention mapped file is shared betwixt multiple processes too thus y'all involve to ensure that it won't acquire corrupted i.e. some regions of retention mapped file non becoming unavailable.


3) One to a greater extent than divergence betwixt conduct too non-direct byte buffers are that former's retention footprint may non endure obvious because they are allocated exterior of Java heap field non-direct buffers consume heap space too are plain of report to garbage collection.


4) You tin depository fiscal establishment check whether a byte buffer is conduct or non-direct yesteryear calling isDirect() method from java.nio.ByteBuffer class. It returns truthful if byte buffer is direct.


 ByteBuffer is ane of the of import shape of Java NIO API Difference betwixt Direct, Non Direct too Mapped ByteBuffer inward Java


These were some differences betwixt direct, non-direct too mapped byte buffers inward Java. If y'all are working inward high majority depression latency systems than  most of the  cases  you volition move alongside either conduct or mapped byte buffers. Since ByteBuffer indexes are integer based, which effectively limits their addressable infinite upwards to 2GB, y'all may desire to depository fiscal establishment check BigByteBuffer shape from Java 1.7 NIO package, which provides long indexes, alternatively, y'all tin too role offsets to map unlike regions of retention mapped file.


That's all on the difference betwixt direct, non-direct too mapped byte buffer inward Java. Just squall back that Direct buffers are allocated exterior heap too they are non inward command of Garbage Collection field non-direct buffers are only a wrapper unopen to byte arrays, located within heap. Memory mapped files tin endure accessed yesteryear using MappedByteBuffer, which is too a conduct buffer. One to a greater extent than affair to squall back is that default club of bytes inward ByteBuffer is BIG_ENDIAN, which way the bytes of a multibyte value are ordered from most meaning to to the lowest degree significant.

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java Non-Blocking IO alongside Java.NIO too Design Patterns


Recommended Book for farther reading
Java Performance The Definitive Guide By Scott Oaks [see here]
Pro Java vii NIO.2 yesteryear Anghel Leonard [see here]