美文网首页
NIO & 零拷贝

NIO & 零拷贝

作者: Cook1fan | 来源:发表于2021-04-14 09:40 被阅读0次
image.png
image.png
image.png

DMA:direct memory access,直接内存拷贝,不使用CPU

image.png
image.png
image.png
image.png
image.png
image.png
// java io 服务器
public class OldIOServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(6666);
        while (true) {
            Socket socket = serverSocket.accept();
            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
            try {
                byte[] byteArray = new byte[4096];
                while (true) {
                    int readCount = dataInputStream.read(byteArray, 0, byteArray.length);
                    if (-1 == readCount) {
                        break;
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

    }
}
public class OldIOClient {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 6666);
        String fileName = "C:\\Users\\32582\\Desktop\\mf.log";
        InputStream inputStream = new FileInputStream(fileName);
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());

        byte[] buffer = new byte[4096];
        long readCount;
        long total = 0;

        long startTime = System.currentTimeMillis();
        while ((readCount = inputStream.read(buffer)) >= 0) {
            total += readCount;
            dataOutputStream.write(buffer);
        }

        System.out.println("发送总字节数:" + total + ", 耗时:" + (System.currentTimeMillis() - startTime));

        dataOutputStream.close();
        socket.close();
        inputStream.close();

    }
}
public class NewIOServer {
    public static void main(String[] args) throws IOException {
        InetSocketAddress address = new InetSocketAddress(6666);
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        ServerSocket serverSocket = serverSocketChannel.socket();
        serverSocket.bind(address);

        // 创建 buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(4096);
        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept();
            int readCount = 0;
            while (-1 != readCount) {
                readCount = socketChannel.read(byteBuffer);
            }
            byteBuffer.rewind(); // 倒带 position = 0 mark 作废
        }
    }
}
public class NewIOClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("localhost", 6666));
        String fileName = "C:\\Users\\32582\\Desktop\\mf.log";
        // 得到一个文件 channel
        FileChannel fileChannel = new FileInputStream(fileName).getChannel();
        // 准备发送
        long startTime = System.currentTimeMillis();
        // 在 linux 下一个transferTo 方法就可以完成传输
        // 在 windows 下一次调用 transferTo 只能发送 8m, 就需要分段传输文件,而且要注意传输时的位置
        // transferTo 底层使用到零拷贝
        long transferCount = fileChannel.transferTo(0, fileChannel.size(), socketChannel);
        System.out.println("发送总字节数:" + transferCount + ", 耗时:" + (System.currentTimeMillis() - startTime));

        fileChannel.close();
    }
}
image.png
image.png

相关文章

  • NIO零拷贝

    先来偷个图,然后扶好小板凳,别摔着了。这是传统IO的文件操作 假设有以下场景,浏览器请求磁盘上的文件a.txt,那...

  • NIO & 零拷贝

    DMA:direct memory access,直接内存拷贝,不使用CPU

  • JVM

    直接内存 使用场景:Unsafe类、NIO零拷贝、Netty的零拷贝、JNI 优点:性能更高 缺点:内存泄漏难排查...

  • NIO与零拷贝

    一、是什么 先来看如下一段代码: 这段代码就是读取一个文件,然后再把它写出去,看起来就几行代码,其实涉及到多次拷贝...

  • NIO-零拷贝

    首先,调用read时,数据文件A拷贝到了kernel模式; 之后,CPU控制将kernel模式数据复制到user模...

  • NIO与零拷贝

    1 零拷贝原理 1.1 传统IO4次拷贝3次切换 2.1优化后3次拷贝2次切换 零拷贝是从操作系统角度看的,是没有...

  • 什么是 NIO?

    1、前言 想必大家肯定被什么公众号什么非阻塞、零拷贝、NIO 之类的搞的头了吧,但其实 Java 的 NIO 概念...

  • Netty之二NIO与零拷贝

    个人专题目录 1. Nio与零拷贝 零拷贝是服务器网络编程的关键,任何性能优化都离不开。在 Java 程序员的世界...

  • Nio中文件映射和零拷贝

    NIO中文件映射和零拷贝 Zero-Copy DMA从拷贝至内核缓冲区 cpu将数据从内核缓冲区拷贝至内核空间(s...

  • JAVA IO专题一:java InputStream和Outp

    笔者相关文章 java NIO读取文件并通过socket发送,最少拷贝了几次?堆外内存和所谓的零拷贝到底是什么关系...

网友评论

      本文标题:NIO & 零拷贝

      本文链接:https://www.haomeiwen.com/subject/alhclltx.html