面试刷题12:zero copy是怎么回事?
文件copy是java的io部分不可忽视的内容。
我是李福春,我在准备面试,今天的问题是:
zero-copy是怎么回事?
操作系统的空间划分为内核态空间, 用户态空间;
内核态空间相对操作系统具备更高的权限和优先级;
用户态空间即普通用户所处空间。
zero-copy指的使用类似java.nio的transforTo方法进行文件copy,文件的copy直接从磁盘到内核态空间,不经过用户态空间,再写到磁盘,减少了io的消耗,避免了不必要的copy 和上下文切换,所以比较高效。
接下来对面试官可能扩展的问题进行一些拓展:
java的文件copy方式
java.io流式copy
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[1024];
int length;
while ((length = fileInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
代码可以运行;copy流程如下图:它不是zero-copy的,需要切换用户态空间和内核态空间,路径比较长,io消耗和上线文切换的消耗比较明显,这是比较低效的copy.
java.nioChannel式copy
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;
/**
* 说明:传统的文件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>0;){
final long transferTo = sourceFileChannel.transferTo(sourceFileChannel.position(), count, targetFileChannel);
count-=transferTo;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
copy过程如下图:明显,不用经过用户态空间,是zero-copy,减少了io的消耗以及上下文切换,比较高效。
Files工具类copy
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();
}
}
}
面试官一般喜欢刨根问底,那么来吧!贴一下源码:
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做了系统级的介绍。
不啰嗦,可以快速通过下图条理化本篇内容,希望对你有所帮助。
原创不易,转载请注明出处。
面试刷题12:zero copy是怎么回事?的更多相关文章
- 安利一个基于Spring Cloud 的面试刷题系统。面试、毕设、项目经验一网打尽
推荐: 接近100K star 的Java学习/面试指南 Github 95k+点赞的Java面试/学习手册.pdf 今天给小伙伴们推荐一个朋友开源的面试刷题系统. 这篇文章我会从系统架构设计层面详解 ...
- 回文的范围——算法面试刷题2(for google),考察前缀和
如果一个正整数的十进制表示(没有前导零)是一个回文字符串(一个前后读取相同的字符串),那么它就是回文.例如,数字5, 77, 363, 4884, 11111, 12121和349943都是回文. 如 ...
- AI面试刷题版
(1)代码题(leetcode类型),主要考察数据结构和基础算法,以及代码基本功 虽然这部分跟机器学习,深度学习关系不大,但也是面试的重中之重.基本每家公司的面试都问了大量的算法题和代码题,即使是商汤 ...
- 有效的括号序列——算法面试刷题4(for google),考察stack
给定一个字符串所表示的括号序列,包含以下字符: '(', ')', '{', '}', '[' and ']', 判定是否是有效的括号序列. 括号必须依照 "()" 顺序表示, & ...
- 相似的RGB颜色——算法面试刷题3(for google),考察二分
在本题中,每个大写字母代表从“0”到“f”的一些十六进制数字. 红绿蓝三元色#AABBCC可以简写为#ABC. 例如,#15c是颜色#1155cc的简写. 现在,假设两种颜色#ABCDEF和#UVWX ...
- 有效单词词广场——算法面试刷题5(for google),考察数学
给定一个单词序列,检查它是否构成一个有效单词广场.一个有效的单词广场应满足以下条件:对于满足0≤k<max(numRows numColumns)的k,第k行和第k列对应的字符串应该相同,. 给 ...
- 面试刷题31:分布式ID设计方案
面试中关于分布式的问题很多.(分布式事务,基本理论CAP,BASE,分布式锁)先来一个简单的. 简单说一下分布式ID的设计方案? 首先要明确在分布式环境下,分布式id的基本要求. 1, 全局唯一,在分 ...
- 面试刷题11:java系统中io的分类有哪些?
随着分布式技术的普及和海量数据的增长,io的能力越来越重要,java提供的io模块提供了足够的扩展性来适应. 我是李福春,我在准备面试,今天的问题是: java中的io有哪几种? java中的io分3 ...
- 面试刷题17:线程两次start()会发生什么?
线程是并发编程的基础元素,是系统调度的最小单元,现代的jvm直接对应了内核线程.为了降低并发编程的门槛,go语言引入了协程. 你好,我是李福春,我在准备面试,今天的题目是? 一个线程两次调用start ...
随机推荐
- vue基础指令了解
Vue了解 """ vue框架 vue是前台框架:Angular.React.Vue vue:结合其他框架优点.轻量级.中文API.数据驱动.双向绑定.MVVM设计模式. ...
- Springboot整合Dubbo和Zookeeper
Dubbo是一款由阿里巴巴开发的远程服务调用框架(RPC),其可以透明化的调用远程服务,就像调用本地服务一样简单.截至目前,Dubbo发布了基于Spring Boot构建的版本,版本号为0.2.0,这 ...
- MySQL树形结构的数据库表设计和查询
1.邻接表(Adjacency List) 实例:现在有一个要存储一下公司的人员结构,大致层次结构如下: 那么怎么存储这个结构?并且要获取以下信息: 1.查询小天的直接上司. 2.查询老宋管理下的直属 ...
- 仿豆瓣首页弹性滑动控件|Axlchen's blog
逛豆瓣的时候看到了这样的控件,觉得挺有趣,遂模仿之 先看看原版的效果 再看看模仿的效果 分析 控件结构分析 由于*ScrollView只能有一个child view,所以整个child view的结构 ...
- 《Java 8实战》读书笔记系列——第三部分:高效Java 8编程(四):使用新的日期时间API
https://www.lilu.org.cn/https://www.lilu.org.cn/ 第十二章:新的日期时间API 在Java 8之前,我们常用的日期时间API是java.util.Dat ...
- C++学习之旅
到现在为止学习C++也已经有一个半月了.一个半个月里我怀着好奇与敬畏一步步的走来,一步步的走向C++的内心深处,也发现了C++"内心的复杂".虽有坎坷,但从未放弃. 我承认,我不是 ...
- git还原历史某一版本
返回上一版本 git reset --hard HEAD^ 常用的命令git refloggit reset --hard "填写版本号" 黄色的就是版本号 其他查看以前版本的命令 ...
- springboot1.5.9整合websocket实现实时显示的小demo
最近由于项目需要实时显示数据库更新的数据变化情况,一开始想过在前端使用ajax异步轮询方法实现,但后面考虑到性能和流量等要求,就放弃该方法而选择使用websocket(毕竟现在springboot整合 ...
- oracle根据特定字符拆分字符串的方法
清洗数据需要将某个字段内以空格分隔的字符串拆分成多行单个的字符串,百度了很多种方法大概归结起来也就这几种方法最为有效,现在把贴出来: 第一种: select regexp_substr('1 2 3' ...
- 06 EntityManager和EntityTransaction
EntityManager 在 JPA 规范中, EntityManager是完成持久化操作的核心对象.实体类作为普通 java对象,只有在调用 EntityManager将其持久化后才会变成持久化对 ...