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

ByteBuffer is i of the of import flat of Java NIO API. It was introduced inward java.nio packet on JDK 1.4, it non alone allows you lot to operate on heap byte arrays but equally good amongst conduct memory, which resides exterior the JVM. There are mainly iii types f ByteBuffer, Direct, Non-Direct as well as mapped byte buffers. You tin dismiss create both conduct 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 master copy divergence betwixt conduct as well as non-direct byte buffers are at that topographic point retentiveness location, non-direct byte buffers are exactly a wrapper approximately byte array as well as they reside inward Java Heap retentiveness piece conduct byte buffer is exterior of JVM as well as retentiveness is non allocated from the heap.

You tin dismiss equally good recollect this fact past times their name, Direct indicates working amongst retentiveness directly. Due to this reason, conduct byte buffers are equally good non affected past times Garbage Collection. MappedByteBuffer is equally good a type of conduct byte buffer, which correspond a memory-mapped part of a file.

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

If you lot dearest books similar me as well as wants to acquire advanced concept e.g. high performance as well as depression latency application development, performance tuning, as well as JVM internals, I propose taking a expect at the Definitive guide to Java Performance, i of the must-read books for Java programmers.




Direct vs Non-direct vs MappedByteBuffer inward Java

As I said ByteBuffer is i of the rattling of import flat inward a high-performance application. It is widely used inward the high-frequency trading application, which strives for rattling depression latency, generally inward sub-microsecond level. When I get-go mentioned close retentiveness mapped file inward Java, I convey outlined some benefits of using those files, as well as ByteBuffer flat is key to operate them. Most of the differences betwixt conduct as well as non-direct ByteBuffer derived from the fact that i is inside heap memory piece other is exterior heap.



1) The get-go divergence betwixt non-direct as well as conduct 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 hold out created past times calling manufactory method allocateDirect() or past times mapping a part of a file straight into retentiveness , known  as MappedByteBuffer.


2) In the illustration of Direct byte buffer, JVM performs native IO operation straight into the buffer, without copying them into whatever intermediate buffer, this makes it rattling attractive for performing high-speed IO functioning on them, but this facility comes amongst care. If a retentiveness mapped file is shared betwixt multiple processes as well as hence you lot involve to ensure that it won't acquire corrupted i.e. some regions of retentiveness mapped file non becoming unavailable.


3) One to a greater extent than divergence betwixt conduct as well as non-direct byte buffers are that former's retentiveness footprint may non hold out obvious because they are allocated exterior of Java heap piece non-direct buffers consume heap space as well as are discipline to garbage collection.


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


 ByteBuffer is i of the of import flat 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 amongst either conduct or mapped byte buffers. Since ByteBuffer indexes are integer based, which effectively limits their addressable infinite upwards to 2GB, you lot may desire to depository fiscal establishment check BigByteBuffer flat from Java 1.7 NIO package, which provides long indexes, alternatively, you lot tin dismiss equally good role offsets to map unlike regions of retentiveness 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 hold out accessed past times using MappedByteBuffer, which is equally good a conduct buffer. One to a greater extent than affair to recollect is that default lodge of bytes inward ByteBuffer is BIG_ENDIAN, which agency 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 amongst Java.NIO as well as Design Patterns


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



Sumber https://javarevisited.blogspot.com/

0 Response to "Difference Betwixt Direct, Not Straight Together With Mapped Bytebuffer Inwards Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel