龙哥网

龙哥网

浅析Java NIO 直接缓冲区和非直接缓冲区_java
2022-03-01

定义

以上是书《深入理解java虚拟机》对直接内存的描述。直接缓冲区用的就是直接内存。

  1. java nio字节缓冲区要么是直接的,要么是非直接的。如果为直接字节缓冲区,则java虚拟机会尽最大努力直接在此缓冲区上执行本机的IO操作,也就是说,在每次调用基础操作系统的一个本机IO操作前后,虚拟机都会尽量避免将内核缓冲区内容复制到用户进程缓冲区中,或者反过来,尽量避免从用户进程缓冲区复制到内核缓冲区中。
  2. 直接缓冲区可以通过调用该缓冲区类的allocateDirect(int capacity) 方法创建,此方法返回的缓冲区进行分配和取消分配所需的成本要高于非直接缓冲区。直接缓冲区的内容驻留在垃圾回收堆之外,因此他们对应用程序内存(JVM内存)需求不大。所以建议直接缓冲区要分配给那些大型,持久(就是缓冲区的数据会被重复利用)的缓冲区,一般情况下,最好仅在直接缓冲区能在程序性能带来非常明显的好处时才分配它们。
  3. 直接缓冲区还可以通过FileCHannel的map()方法将文件区域映射到内存中来创建,该方法返回MappedByteBuffer。java平台的实现有助于通过JNI本地代码创建直接字节缓冲区,如果以上这些缓冲区中某个缓冲区实例指向的是不可访问的内存区域,则试图方法该区域不会更改缓冲区的内容,并且会在访问期间或者稍后的某个时间导致报出不确定性异常。
  4. 字节缓冲区是直接缓冲区还是非直接缓冲区可以通过调用其isDIrect()方法来判断。

基于NIO的本地IO直接内存使用:

传统IO对文件数据进行读写的流程:

流程说明(以上是应用程序完成一次文件拷贝的流程):

  1. 应用进程发起一个读请求系统调用,然后进程切换到内核态。
  2. DMA把磁盘数据复制到内核缓冲区中。
  3. 内核把缓冲区数据复制到用户缓冲区中。
  4. 进程切换到用户态。
  5. 应用进程发起一个写请求系统调用,然后进程切换到内核态。
  6. 内核把用户缓冲区数据复制到内核缓冲区。
  7. DMA把内核缓冲区数据复制到磁盘上。
  8. 返回。

以上流程一共进行了四次上下文切换,四次数据拷贝。

使用mmap实现对传统文件IO优化。

mmap:通过把内核缓冲区和用户缓冲区映射在物理内存上映射为同一地址空间。这样就不用对数据进行复制了。

这个是传统的:

使用mmap后的IO大致流程:

数据拷贝次数从4次缩短到了两次。

相关API demo以及比较:详细api解释可以查看Java NIO学习篇之通道FileChannel详解

//使用直接缓冲区API进行一个700多M的文件进行拷贝
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("直接缓冲区耗时:" + (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("非缓冲区耗时:" + (System.currentTimeMillis()-start));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

执行结果:

 

免责声明
本站部分资源来源于互联网 如有侵权 请联系站长删除
龙哥网是优质的互联网科技创业资源_行业项目分享_网络知识引流变现方法的平台为广大网友提供学习互联网相关知识_内容变现的方法。