Java NIO-Direct and non-direct buffers

Source: Internet
Author: User

Direct buffers vs. non-direct buffers:

Non-direct buffers: allocates buffers through the allocate () method, setting buffers in the JVM's memory
Direct buffer: Allocates a direct buffer through the Allocatedirect () method, establishing the buffer in physical memory. can improve efficiency

    1. The byte buffers are either direct or non-direct. If it is a direct byte buffer, the Java virtual machine will do its best to perform native I/O operations directly on this buffer in the machine. That is, before (or after) each call to a native I/O operation of the underlying operating system,
    2. The virtual machine will try to avoid copying the contents of the buffer into the intermediate buffer (or copying the contents from the buffer in between).
    3. A direct byte buffer can be created by calling the Allocatedirect () factory method of this class. The buffer returned by this method is typically higher than the non-direct buffer for allocation and deallocation. The contents of the direct buffers can reside outside the regular garbage collection heap, so their impact on the memory requirements of the application may not be obvious. Therefore, it is recommended that the direct buffers be allocated primarily to large, persistent buffers that are susceptible to native I/O operations of the underlying system. In general, it is best to assign them only if the direct buffers can bring significant benefits in terms of program performance.
    4. Direct byte buffers can also be created by mapping the file area directly into memory through the FileChannel map () method. The method returns Mappedbytebuffer. The implementation of the Java platform facilitates the creation of direct byte buffers from native code through JNI. If a buffer instance in these buffers refers to an inaccessible area of memory, attempting to access the zone does not change the contents of the buffer and will cause an indeterminate exception to be thrown during or at a later time during the visit.
    5. The byte buffer is either a direct buffer or a non-direct buffer that can be determined by calling its Isdirect () method. This method is provided for the ability to perform explicit buffer management in performance-critical code.

Non-direct buffers:

Direct buffers:

code example:

    @Test    publicvoid  test3 () {        // Allocate direct buffers        Bytebuffer buf = bytebuffer.allocatedirect (1024x768);        System.out.println (Buf.position ());        System.out.println (Buf.limit ());        System.out.println (Buf.capacity ());        System.out.println (Buf.isdirect ());    }

Java NIO-Direct and non-direct buffers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.