Difference Betwixt Direct, Not At 1 Time Together With Mapped Bytebuffer Inwards Java

ByteBuffer is ane of the of import course of teaching of Java NIO API. It was introduced inward java.nio bundle on JDK 1.4, it non alone allows you lot to operate on heap byte arrays but besides alongside remove memory, which resides exterior the JVM. There are mainly 3 types f ByteBuffer, Direct, Non-Direct as well as mapped byte buffers. You tin dismiss create both remove as well as non-direct buffers using java.nio.ByteBuffer class, piece MappedByteBuffer is a subclass of ByteBuffer, which is created past times FileChannel.map() method, to operate on memory mapped file. The mind divergence betwixt remove as well as non-direct byte buffers are at that topographic point retention location, non-direct byte buffers are precisely a wrapper approximately byte array as well as they reside inward Java Heap retention piece remove byte buffer is exterior of JVM as well as retention is non allocated from the heap.

You tin dismiss besides recollect this fact past times their name, Direct indicates working alongside retention directly. Due to this reason, remove byte buffers are besides non affected past times Garbage Collection. MappedByteBuffer is besides a type of remove byte buffer, which stand upward for a memory-mapped part of a file.

In this Java NIO tutorial, you lot volition come across a twain of to a greater extent than differences betwixt direct, non-direct as well as mapped byte buffers, which volition assist you lot to empathise the concept as well as their usage better.

If you lot beloved books similar me as well as wants to larn advanced concept e.g. high performance as well as depression latency application development, performance tuning, as well as JVM internals, I propose taking a await 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 course of teaching inward a high-performance application. It is widely used inward the high-frequency trading application, which strives for really depression latency, generally inward sub-microsecond level. When I kickoff mentioned nearly retention mapped file inward Java, I bring outlined some benefits of using those files, as well as ByteBuffer course of teaching is key to operate them. Most of the differences betwixt remove as well as non-direct ByteBuffer derived from the fact that ane is inside heap memory piece other is exterior heap.



1) The kickoff divergence betwixt non-direct as well as remove byte buffer comes from the fact, how you lot create them. You tin dismiss create non-direct byte buffer either past times allocating infinite for buffer's content or past times wrapping an existing byte array into a buffer. While a Direct byte buffer may move created past times calling mill method allocateDirect() or past times mapping a part of a file remove into retention , known  as MappedByteBuffer.


2) In the representative of Direct byte buffer, JVM performs native IO operation remove into the buffer, without copying them into whatever 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 as well as therefore you lot 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 remove as well as non-direct byte buffers are that former's retention footprint may non move obvious because they are allocated exterior of Java heap piece non-direct buffers consume heap space as well as are champaign of written report to garbage collection.


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


 ByteBuffer is ane of the of import course of teaching of Java NIO API Difference betwixt Direct, Non Direct as well as Mapped ByteBuffer inward Java


These were some differences betwixt direct, non-direct as well as mapped byte buffers inward Java. If you lot are working inward high mass depression latency systems than  most of the  cases  you volition operate alongside either remove or mapped byte buffers. Since ByteBuffer indexes are integer based, which effectively limits their addressable infinite upward to 2GB, you lot may desire to depository fiscal establishment check BigByteBuffer course of teaching from Java 1.7 NIO package, which provides long indexes, alternatively, you lot tin dismiss besides role offsets to map dissimilar regions of retention mapped file.


That's all on the difference betwixt direct, non-direct as well as mapped byte buffer inward Java. Just recollect that Direct buffers are allocated exterior heap as well as they are non inward command of Garbage Collection piece non-direct buffers are merely a wrapper approximately byte arrays, located within heap. Memory mapped files tin dismiss move accessed past times using MappedByteBuffer, which is besides a remove buffer. One to a greater extent than affair to recollect is that default lodge of bytes inward ByteBuffer is BIG_ENDIAN, which way the bytes of a multibyte value are ordered from most pregnant to to the lowest degree significant.

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


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


Belum ada Komentar untuk "Difference Betwixt Direct, Not At 1 Time Together With Mapped Bytebuffer Inwards Java"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel