Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are Java NIO direct buffer and indirect buffer?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "Java NIO direct buffer and indirect buffer is what", the explanation content in the article is simple and clear, easy to learn and understand, please follow the idea of Xiaobian slowly in-depth, together to study and learn "Java NIO direct buffer and indirect buffer is what"!

defined

Java nio byte buffers are either direct or indirect. In the case of a direct byte buffer, the java virtual machine does its best to perform native IO operations directly on this buffer, that is, the virtual machine tries to avoid copying the contents of the kernel buffer into the user process buffer, or vice versa, before and after each call to a native IO operation of the underlying operating system.

Direct buffers can be created by calling the buffer class's allocateDirect(int capacity) method, which returns buffers that are more costly to allocate and deallocate than indirect buffers. The contents of direct buffers reside outside of the garbage collection heap, so they require little application memory (JVM memory). Therefore, it is recommended that direct buffers be allocated to large, persistent buffers (i.e., buffers whose data will be reused), and that they be allocated only when there is a significant benefit to program performance.

Direct buffers can also be created by mapping file regions into memory via FileCHannel's map() method, which returns MappedByteBuffer. Java platform implementations facilitate the creation of direct byte buffers through JNI native code, and if one of these buffer instances points to an inaccessible memory region, attempting to access that region does not change the contents of the buffer and causes an indeterminate exception to be raised during access or at some later time.

Whether a byte buffer is direct or indirect can be determined by calling its isDIrect() method.

NIO-based local IO direct memory usage:

The process of traditional IO reading and writing file data:

Process description (above is the process of completing a file copy by the application):

The application process initiates a read request system call, and then the process switches to kernel mode.

DMA copies disk data into kernel buffers.

The kernel copies the buffer data into the user buffer.

The process switches to user mode.

The application process initiates a write request system call, and then the process switches to kernel mode.

The kernel copies the user buffer data into the kernel buffer.

DMA copies kernel buffer data to disk.

Return.

The above process involves four context switches and four data copies.

Use mmap to optimize traditional file IO.

mmap: By mapping kernel buffers and user buffers into the same address space on physical memory. This eliminates the need to duplicate data.

This is traditional:

The approximate IO flow after using mmap:

The number of copies of data was reduced from four to two.

Related API demo and comparison: detailed api explanation can be viewed Java NIO learning chapter channel FileChannel detailed explanation

//public static void testDirect(){ try { long start = System.currentTimeMillis(); FileChannel srcFileChannel = FileChannel.open(Paths.get("C:\\Users\\Yehaocong\\Desktop\\test\\95462017-1-64.flv"), StandardOpenOption.READ); FileChannel destFileChannel = FileChannel.open(Paths.get("C:\\Users\\Yehaocong\\Desktop\\test\\95462017-1-64-cp1.flv"),StandardOpenOption.CREATE, StandardOpenOption.WRITE,StandardOpenOption.READ); MappedByteBuffer srcByteBuffer = srcFileChannel.map(FileChannel.MapMode.READ_ONLY,0,srcFileChannel.size()); MappedByteBuffer descByteBuffer = destFileChannel.map(FileChannel.MapMode.READ_WRITE,0,srcFileChannel.size()); descByteBuffer.put(srcByteBuffer); srcFileChannel.close(); destFileChannel.close(); System.out.println("Direct Buffer Time: " + (System.currentTimeMillis()-start)); } catch (IOException e) { e.printStackTrace(); } }public static void testSimpleIO(){ try { long start = System.currentTimeMillis(); FileChannel srcFileChannel = FileChannel.open(Paths.get("C:\\Users\\Yehaocong\\Desktop\\test\\95462017-1-64.flv"), StandardOpenOption.READ); FileChannel destFileChannel = FileChannel.open(Paths.get("C:\\Users\\Yehaocong\\Desktop\\test\\95462017-1-64-cp.flv"),StandardOpenOption.CREATE, StandardOpenOption.WRITE,StandardOpenOption.READ); ByteBuffer byteBuffer = ByteBuffer.allocate((int) srcFileChannel.size()); while (srcFileChannel.read(byteBuffer)!=- 1){ byteBuffer.flip(); destFileChannel.write(byteBuffer); byteBuffer.clear(); } srcFileChannel.close(); destFileChannel.close(); System.out.println("non-buffer time: " + (System.currentTimeMillis()-start)); } catch (IOException e) { e.printStackTrace(); } }

Implementation results:

Thank you for reading, the above is "Java NIO direct buffer and indirect buffer is what" the content, after the study of this article, I believe we have a deeper understanding of Java NIO direct buffer and indirect buffer is what this problem, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report