Java-nio (iii): direct buffer vs. non-direct buffer

Source: Internet
Author: User

    • The concept of direct buffers versus non-direct buffers:

1) Non-direct buffer: through

Static Bytebuffer allocate (int capacity)

Created buffers that are created in memory in the JVM, before or after each call to a native IO of the underlying operating system, the virtual machine copies the contents of the buffer to the intermediate buffer (or copies the contents from the intermediate buffer), and the contents of the buffer reside within the JVM, so it is easy to destroy, but consumes the JVM's memory overhead , there is a copy operation during processing.

Non-direct buffer write steps:

1. Create a temporary direct Bytebuffer object.
2. Copy the contents of the non-direct buffer into the temporary buffer.
3. Use a temporary buffer to perform low-level I/O operations.
4. The temporary buffer object leaves the scope and eventually becomes garbage collected data.

2) Direct buffer: through

Static Bytebuffer allocatedirect (int capacity)

Creates buffers that open memory outside of the JVM's memory, and the virtual machine avoids copying the contents of the buffer to the intermediate buffer (or copying the contents from the intermediate buffer) before or after each call to a native IO of the underlying operating system, and the contents of the buffer reside in physical memory, and the copy process is less. If you need to recycle buffers, using a direct buffer can greatly improve performance. While a direct buffer enables the JVM to perform efficient I/O operations, it uses memory that is allocated by the operating system, bypassing the JVM stack, and creating and destroying more overhead than buffers on the stack.

    • The difference between a direct buffer and a non-direct buffer:
    1. The byte buffers are either direct or non-direct. If it is a direct byte buffer, the Java virtual machine tries its best to perform native I/O operations directly on this buffer. That is, the virtual machine tries to avoid copying the contents of the buffer into the intermediate buffer (or copying the contents from the buffer in between) before each call to a native I/O operation of the underlying operating system.
    2. 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.
    3. 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.
    4. 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.
    • the direct buffer differs from the non-direct buffer graph schematic:

Java-nio (iii): direct buffer vs. non-direct buffer

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.