Java中三种零拷贝的实现示例以及对比详解

 更新时间:2023年12月21日 10:49:01   作者:Colins~  
这篇文章主要介绍了Java中三种零拷贝的实现示例以及对比详解,本文主要是介绍几种零拷贝的实现示例,以及与最传统的做一个对比,看看在效率上到底有多大的提升,需要的朋友可以参考下

简介

本文主要是介绍几种零拷贝的实现示例,以及与最传统的做一个对比,看看在效率上到底有多大的提升

好了,废话不多说直接干,本章例子是通过网络IO传输一个8M大小的文件,对比传输效率,由于服务端接收端不需要修改,所以我们先上服务端代码:

public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = new ServerSocket(8080);
    System.out.println("服务端:等待连接");
    Socket accept = serverSocket.accept();
    System.out.println("服务端:" + accept.getRemoteSocketAddress() + "已连接");
    File file = new File("C:\\Users\\Administrator\\Desktop\\ioTest.txt");
    if(!file.exists()){
        file.createNewFile();
    }
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    InputStream bufferedInputStream = accept.getInputStream();
    byte[] bytes = new byte[2048];
    int read;
    while ((read = bufferedInputStream.read(bytes,0,2048)) != -1) {
        fileOutputStream.write(bytes);
    }
    OutputStream outputStream = accept.getOutputStream();
    outputStream.write("接收完毕".getBytes());
    accept.shutdownOutput();
    fileOutputStream.close();
    outputStream.close();
    bufferedInputStream.close();
    accept.close();
}

传统实现

正常的socket传输,耗时:46ms

public static void normal() throws IOException {
        Socket socket = new Socket("127.0.0.1", 8080);
        OutputStream outputStream = socket.getOutputStream();
        InputStream inputStream = socket.getInputStream();
        long start = System.currentTimeMillis();
        File file = new File("C:\\Users\\Administrator\\Desktop\\222.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bytes1 = new byte[2048];
        while (fileInputStream.read(bytes1, 0, 2048) != -1) {
            outputStream.write(bytes1);
        }
        socket.shutdownOutput();
        System.out.println("耗时:" + (System.currentTimeMillis() - start));
        byte[] bytes = new byte[1024];
        String message = "";
        int read;
        while ((read = inputStream.read(bytes)) != -1) {
            message += new String(bytes, 0, read);
        }
        System.out.println("服务端发来消息->" + message);
        inputStream.close();
        outputStream.close();
        socket.close();
    }

MMAP

MMAP原理就是建立了一个文件映射,划分了一个虚拟空间,往这个空间写数据,少了一次拷贝

缺点:空间有限

实践案例:RocketMq

耗时:32ms

public static void mmp() throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("127.0.0.1", 8080));
        long start = System.currentTimeMillis();
        Path path = Paths.get("C:\\Users\\Administrator\\Desktop\\222.txt");
        FileChannel open = FileChannel.open(path, StandardOpenOption.READ);
        MappedByteBuffer map = open.map(FileChannel.MapMode.READ_ONLY, 0, open.size());
        socketChannel.write(map);
        socketChannel.shutdownOutput();
        System.out.println("耗时:" + (System.currentTimeMillis() - start));
        ByteBuffer allocate = ByteBuffer.allocate(1024);
        int read = socketChannel.read(allocate);
        if (read > 0) {
            allocate.flip();
            byte[] bytes = new byte[allocate.remaining()];
            allocate.get(bytes);
            System.out.println("服务端发来消息:" + new String(bytes));
        }
        socketChannel.close();
    }

transferTo

原理就是两个通道之间直接传输数据,根据系统支持程度,少了1-2次拷贝

缺点:局限于文件通道

实践案例:Netty、Kafka

耗时:18ms

 public static void transferTo() throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("127.0.0.1", 8080));
        long start = System.currentTimeMillis();
        Path path = Paths.get("C:\\Users\\Administrator\\Desktop\\222.txt");
        FileChannel open = FileChannel.open(path, StandardOpenOption.READ);
        long l = open.transferTo(0, open.size(), socketChannel);
        socketChannel.shutdownOutput();
        System.out.println("耗时:" + (System.currentTimeMillis() - start));
        ByteBuffer allocate = ByteBuffer.allocate(1024);
        int read = socketChannel.read(allocate);
        if (read > 0) {
            allocate.flip();
            byte[] bytes = new byte[allocate.remaining()];
            allocate.get(bytes);
            System.out.println("服务端发来消息:" + new String(bytes));
        }
        socketChannel.close();
    }

堆外内存

原理直接使用堆外内存,少了一次拷贝

缺点:堆外内存开启耗时,此内存不受JVM控制,如垃圾回收等

实践案例:Netty

耗时:26ms

public static void outSide() throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("127.0.0.1", 8080));
        long start = System.currentTimeMillis();
        Path path = Paths.get("C:\\Users\\Administrator\\Desktop\\222.txt");
        FileChannel open = FileChannel.open(path, StandardOpenOption.READ);
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect((int) open.size());
        open.read(byteBuffer);
        byteBuffer.flip();
        socketChannel.write(byteBuffer);
        socketChannel.shutdownOutput();
        System.out.println("耗时:" + (System.currentTimeMillis() - start));
        ByteBuffer allocate = ByteBuffer.allocate(1024);
        int read = socketChannel.read(allocate);
        if (read > 0) {
            allocate.flip();
            byte[] bytes = new byte[allocate.remaining()];
            allocate.get(bytes);
            System.out.println("服务端发来消息:" + new String(bytes));
        }
        socketChannel.close();
    }

总结

耗时统计不完全准确,都是多次取平均,具体使用哪种需要看场景来

到此这篇关于Java中三种零拷贝的实现示例以及对比详解的文章就介绍到这了,更多相关Java零拷贝方式对比内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java数据机构中关于并查集的详解

    Java数据机构中关于并查集的详解

    并查集是一种树型的数据结构,用于处理一些不相交集合的合并及查询问题,如果你还不了解并查集,请看接下来的文章,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值
    2021-09-09
  • Java中MessageFormat的使用详解

    Java中MessageFormat的使用详解

    本文主要介绍了Java中MessageFormat的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • 基于java HashMap插入重复Key值问题

    基于java HashMap插入重复Key值问题

    这篇文章主要介绍了基于java HashMap插入重复Key值问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • MyBatis-Plus联表查询(Mybatis-Plus-Join)的功能实现

    MyBatis-Plus联表查询(Mybatis-Plus-Join)的功能实现

    mybatis-plus作为mybatis的增强工具,简化了开发中的数据库操作,这篇文章主要介绍了MyBatis-Plus联表查询(Mybatis-Plus-Join),需要的朋友可以参考下
    2022-08-08
  • Java日常练习题,每天进步一点点(14)

    Java日常练习题,每天进步一点点(14)

    下面小编就为大家带来一篇Java基础的几道练习题(分享)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望可以帮到你
    2021-07-07
  • java关于Date日期类型的大小比较

    java关于Date日期类型的大小比较

    这篇文章主要介绍了java关于Date日期类型的大小比较,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • Java自动化实现PowerPoint转换为PDF

    Java自动化实现PowerPoint转换为PDF

    在日常的企业运营和个人工作中,PowerPoint(PPT/PPTX)文件因其强大的演示功能而被广泛使用,本文将详细介绍如何利用 Spire.Presentation for Java 库,通过简洁高效的代码实现 PowerPoint 到 PDF 的转换,需要的可以了解下
    2025-08-08
  • Java抽象类Abstract Class示例代码详解

    Java抽象类Abstract Class示例代码详解

    Java中的抽象类(Abstract Class)是面向对象编程中的重要概念,它通过abstract关键字声明,用于定义一组相关类的公共行为和属性,下面详细介绍Java抽象类Abstract Class,感兴趣的朋友一起看看吧
    2025-09-09
  • 基于spring-security 401 403错误自定义处理方案

    基于spring-security 401 403错误自定义处理方案

    这篇文章主要介绍了基于spring-security 401 403错误自定义处理方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • springboot整合vue实现上传下载文件

    springboot整合vue实现上传下载文件

    这篇文章主要为大家详细介绍了springboot整合vue实现上传下载文件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11

最新评论