首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >面试刷题12:zero copy是怎么回事?

面试刷题12:zero copy是怎么回事?

作者头像
李福春
发布2025-07-01 16:33:14
发布2025-07-01 16:33:14
7800
代码可运行
举报
运行总次数:0
代码可运行
图片
图片

文件copy是java的io部分不可忽视的内容。 我是李福春,我在准备面试,今天的问题是: zero-copy是怎么回事? 操作系统的空间划分为内核态空间, 用户态空间; 内核态空间相对操作系统具备更高的权限和优先级; 用户态空间即普通用户所处空间。 zero-copy指的使用类似java.nio的transforTo方法进行文件copy,文件的copy直接从磁盘到内核态空间,不经过用户态空间,再写到磁盘,减少了io的消耗,避免了不必要的copy 和上下文切换,所以比较高效。 接下来对面试官可能扩展的问题进行一些拓展:

java的文件copy方式

java.io流式copy

代码语言:javascript
代码运行次数:0
运行
复制
package org.example.mianshi.filecopy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;

/**
 * 说明:传统的文件copy
 * @author carter
 * 创建时间:2020年03月26日 9:32 上午
 **/

public class JioFileCopyApp {

    public static void main(String[] args) {

        final File d = new File("/data/appenvs/denv.properties");
        final File s = new File("/data/appenvs/env.properties");

        System.out.println("source file content :" + s.exists());
        System.out.println("target file content :" + d.exists());

        System.out.println("source content:");
        try {
            Files.lines(s.toPath()).forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("do file copy !");

        copy(s, d);

        System.out.println("target file content :" + d.exists());
        System.out.println("target content:");
        try {
            Files.lines(d.toPath()).forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void copy(File s, File d) {

        try (
                final FileInputStream fileInputStream = new FileInputStream(s);

                final FileOutputStream fileOutputStream = new FileOutputStream(d)
        ) {

            byte[] buffer = new byte[];
            int length;
            while ((length = fileInputStream.read(buffer)) > ) {
                fileOutputStream.write(buffer, , length);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

代码可以运行;copy流程如下图:它不是zero-copy的,需要切换用户态空间和内核态空间,路径比较长,io消耗和上线文切换的消耗比较明显,这是比较低效的copy.

图片
图片

java.nioChannel式copy

代码语言:javascript
代码运行次数:0
运行
复制
package org.example.mianshi.filecopy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;

/**
 * 说明:zero-copy
 * @author carter
 * 创建时间:2020年03月26日 9:32 上午
 **/

public class JnioFileCopyApp {

    public static void main(String[] args) {

        final File d = new File("/data/appenvs/ndenv.properties");
        final File s = new File("/data/appenvs/env.properties");

        System.out.println(s.getAbsolutePath() + "source file content :" + s.exists());
        System.out.println(d.getAbsolutePath() +"target file content :" + d.exists());

        System.out.println("source content:");
        try {
            Files.lines(s.toPath()).forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("do file copy !");

        copy(s, d);

        System.out.println(d.getAbsolutePath() +"target file content :" + d.exists());
        System.out.println("target content:");
        try {
            Files.lines(d.toPath()).forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void copy(File s, File d) {

        try (
                final FileChannel sourceFileChannel = new FileInputStream(s).getChannel();

                final FileChannel targetFileChannel = new FileOutputStream(d).getChannel()
        ) {

           for (long count= sourceFileChannel.size();count>;){

               final long transferTo = sourceFileChannel.transferTo(sourceFileChannel.position(), count, targetFileChannel);

               count-=transferTo;

           }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

copy过程如下图:明显,不用经过用户态空间,是zero-copy,减少了io的消耗以及上下文切换,比较高效。

图片
图片

Files工具类copy

代码语言:javascript
代码运行次数:0
运行
复制
package org.example.mianshi.filecopy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

/**
 * 说明:Files的文件copy
 * @author carter
 * 创建时间:2020年03月26日 9:32 上午
 **/

public class FilesFileCopyApp {

    public static void main(String[] args) {

        final File d = new File("/data/appenvs/fenv.properties");
        final File s = new File("/data/appenvs/env.properties");

        System.out.println("source file content :" + s.exists());
        System.out.println("target file content :" + d.exists());

        System.out.println("source content:");
        try {
            Files.lines(s.toPath()).forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("do file copy !");

        copy(s, d);

        System.out.println("target file content :" + d.exists());
        System.out.println("target content:");
        try {
            Files.lines(d.toPath()).forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void copy(File s, File d) {

        try {
            Files.copy(s.toPath(),d.toPath(), StandardCopyOption.COPY_ATTRIBUTES);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

面试官一般喜欢刨根问底,那么来吧!贴一下源码:

代码语言:javascript
代码运行次数:0
运行
复制
 public static Path copy(Path source, Path target, CopyOption... options)
        throws IOException
    {
        FileSystemProvider provider = provider(source);
        if (provider(target) == provider) {
            // same provider
            provider.copy(source, target, options);
        } else {
            // different providers
            CopyMoveHelper.copyToForeignTarget(source, target, options);
        }
        return target;
    }

底层通过SPI,即ServiceLoader的方式加载不同文件系统的本地处理代码。 分类如下:

图片
图片

我们使用最多的UnixFsProvider,实际上是 直接从 用户态空间copy到用户态空间,使用了本地方法内联加持优化,但是它不是zero-copy, 但是性能也不会太差。

如何提高io的效率

1, 使用缓存,减少io的操作次数; 2,使用zero-copy,即类似 java.nio的 transferTo方法进行copy; 3, 减少传输过程中不必要的转换,比如编解码,最好直接二进制传输;

buffer

buffer的类层级图如下:

图片
图片

除了bool其他7个原生类型都有对应的Buffer; 面试官如果问细节,先说4个属性, capacity, limit ,position, mark 再描述byteBuffer的读写流程。 然后是DirectBuffer,这个是直接操作堆外内存,比较高效。但是用好比较困难,除非是流媒体的行业,不会问的这么细,直接源码好好准备,问一般也是技术专家来问你了。

小结

本篇回答了什么是zero-copy,然后介绍了java体系实现文件copy的3种方式,(扩展的第三方库不算在内); 然后简要介绍了如何提高io效率的三种方法,以及提高内存利用率的Buffer做了系统级的介绍。 不啰嗦,可以快速通过下图条理化本篇内容,希望对你有所帮助。

图片
图片
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-04-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 李福春持续输出 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • java的文件copy方式
    • java.io流式copy
    • java.nioChannel式copy
    • Files工具类copy
  • 如何提高io的效率
  • buffer
  • 小结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档