Difference Betwixt Direct, Not Straightaway In Addition To Mapped Bytebuffer Inwards Java

ByteBuffer is 1 of the of import cast of Java NIO API. It was introduced inwards java.nio bundle on JDK 1.4, it non exclusively allows you lot to operate on heap byte arrays but equally good alongside conduct memory, which resides exterior the JVM. There are mainly 3 types f ByteBuffer, Direct, Non-Direct together with mapped byte buffers. You tin mail away create both conduct together with 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 principal departure betwixt conduct together with non-direct byte buffers are at that spot retentiveness location, non-direct byte buffers are exactly a wrapper some byte array together with they reside inwards Java Heap retentiveness piece conduct byte buffer is exterior of JVM together with retentiveness is non allocated from the heap.

You tin mail away equally good think this fact past times their name, Direct indicates working alongside 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 stand upwards for a memory-mapped part of a file.

In this Java NIO tutorial, you lot volition meet a span of to a greater extent than differences betwixt direct, non-direct together with mapped byte buffers, which volition aid you lot to empathise the concept together with their usage better.

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




Direct vs Non-direct vs MappedByteBuffer inwards Java

As I said ByteBuffer is 1 of the really of import cast inwards a high-performance application. It is widely used inwards the high-frequency trading application, which strives for really depression latency, generally inwards sub-microsecond level. When I commencement mentioned well-nigh retentiveness mapped file inwards Java, I convey outlined some benefits of using those files, together with ByteBuffer cast is key to operate them. Most of the differences betwixt conduct together with non-direct ByteBuffer derived from the fact that 1 is inside heap memory piece other is exterior heap.



1) The commencement departure betwixt non-direct together with conduct byte buffer comes from the fact, how you lot create them. You tin mail away 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 upwards created past times calling mill method allocateDirect() or past times mapping a part of a file conduct into retentiveness , known  as MappedByteBuffer.


2) In the example of Direct byte buffer, JVM performs native IO operation conduct 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 retentiveness mapped file is shared betwixt multiple processes therefore 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 departure betwixt conduct together with non-direct byte buffers are that former's retentiveness footprint may non hold upwards obvious because they are allocated exterior of Java heap piece non-direct buffers consume heap space together with are dependent plain to garbage collection.


4) You tin mail away banking concern 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 1 of the of import cast of Java NIO API Difference betwixt Direct, Non Direct together with Mapped ByteBuffer inwards Java


These were some differences betwixt direct, non-direct together with mapped byte buffers inwards Java. If you lot are working inwards high majority depression latency systems than  most of the  cases  you volition piece of occupation alongside 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 banking concern check BigByteBuffer cast from Java 1.7 NIO package, which provides long indexes, alternatively, you lot tin mail away equally good operate offsets to map unlike regions of retentiveness mapped file.


That's all on the difference betwixt direct, non-direct together with mapped byte buffer inwards Java. Just think that Direct buffers are allocated exterior heap together with they are non inwards command of Garbage Collection piece non-direct buffers are but a wrapper some byte arrays, located within heap. Memory mapped files tin mail away hold upwards accessed past times using MappedByteBuffer, which is equally good a conduct buffer. One to a greater extent than affair to think is that default lodge of bytes inwards 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 together with 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]


Komentar

Postingan populer dari blog ini

2 Ways To Banking Concern Tally If A String Is Rotation Of Other Inward Java?

How To Convert String To Integer To String Inward Coffee Amongst Example

How To Induce Chrome, Firefox Blurry, Over Bright, Fading Afterwards Windows Ten Update