java NIO - DirectBuffer 和 HeapBuffer
问题 :
- DirectBuffer 属于堆外存,那应该还是属于用户内存,而不是内核内存?
- FileChannel 的read(ByteBuffer dst)函数,write(ByteBuffer src)函数中,如果传入的参数是HeapBuffer类型,则会临时申请一块DirectBuffer,进行数据拷贝,而不是直接进行数据传输,这是出于什么原因?
DirectBuffer
Java | native
|
DirectByteBuffer | malloc'd
[ address ] -+-> [ data ]
|
DirectByteBuffer 自身是一个Java对象,在Java堆中;而这个对象中有个long类型字段address,记录着一块调用 malloc() 申请到的native memory。DirectByteBuffer 自身是(Java)堆内的,它背后真正承载数据的buffer是在(Java)堆外——native memory中的。这是 malloc() 分配出来的内存,是用户态的。(来自参考文章R大的回答)
DirectBuffer 和 HeapBuffer
两个都是Buffer ,不同的是前者使用的是堆外内存,后者时候的是 JVM 堆内内存。在使用 FileChannel 读写的时候内部实现就有点不同了。以下是FileChannel使用代码
public static void main(String[] args) throws Exception{
RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
FileChannel channel = aFile.getChannel();
String newData = "New String to write to file..." + System.currentTimeMillis();
// HeapByteBuffer
ByteBuffer buf = ByteBuffer.allocate(48);
// DirectByteBuffer
ByteBuffer dirctBuf = ByteBuffer.allocateDirect(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
while(buf.hasRemaining()) {
channel.write(buf);
}
}
//读取地址
FileInputStream fis = new FileInputStream("C:\\CloudMusic\\Circadian Eyes - Ferris Wheel.mp3");
//写出地址
FileOutputStream fos = new FileOutputStream("D:\\etc\\cas\\logs\\cas_audit.log");
FileChannel fc = fis.getChannel();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
//回刷回磁盘
mbb.flip();
fos.flush();
fc.close();
fis.close();
如果上面的代码channel.write传入的参数是HeapBuffer类型,则会临时申请一块DirectBuffer,将HeapBuffer中的数据进行数据拷贝到堆外内存,然后剩下就是对DirectBuffer进行IO操作,为什么直接使用HeapBuffer拷贝数据到内核中,然后进行IO操作呢?这是因为如果要把一个Java里的 byte[] 对象的引用传给native代码,让native代码直接访问数组的内容的话,就必须要保证native代码在访问的时候这个 byte[] 对象不能被移动,也就是要被“pin”(钉)住。而虚拟机的GC 算法会移动对象,导致地址会变化,那么后续就会产生错误。详细的见参考资料R大的回答。 OpenJDK的 sun.nio.ch.IOUtil.write(FileDescriptor fd, ByteBuffer src, long position, NativeDispatcher nd) 的实现。
static int write(FileDescriptor fd, ByteBuffer src, long position,
NativeDispatcher nd)
throws IOException
{
if (src instanceof DirectBuffer)
return writeFromNativeBuffer(fd, src, position, nd); // Substitute a native buffer
int pos = src.position();
int lim = src.limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
try {
bb.put(src);
bb.flip();
// Do not update src until we see how many bytes were written
src.position(pos); int n = writeFromNativeBuffer(fd, bb, position, nd);
if (n > 0) {
// now update src
src.position(pos + n);
}
return n;
} finally {
Util.offerFirstTemporaryDirectBuffer(bb);
}
}
MappedByteBuffer
MappedByteBuffer 是 DirectBuffer 的父类,它的读写性能比HeapByteBuffer要高(不然FileChannel 内部实现中也不会用DirectByteBuffer进行操作)。MappedByteBuffer 内部原理主要和操作系统的虚拟存储有关,更加直接的联系就是页表相关的知识,先阅读以下这篇文章。
补充
关于 Heap memory 和 Native memory的解释,来自stackoverflow
Heap memory: memory within the JVM process that is managed by the JVM to represent Java objects
Native memory/Off-heap: is memory allocated within the processes address space that is not within the heap.
Direct memory: is similar to native, but also implies that an underlying buffer within the hardware is being shared. For example buffer within the network adapter or graphics display. The goal here is to reduce the number of times the same bytes is being copied about in memory.
Finally, depending upon the OS then extra native allocations (assigning of the memory address space) can be carried out via Unsafe alloc and/or by memory mapping a file. Memory mapping a file is especially interesting as it can easily allocate more memory than the machine currently has as physical ram. Also note, that the total address space limit is restricted by the size of a pointer being used, a 32bit pointer cannot go outside of 4GB. Period.
参考资料
- https://www.zhihu.com/question/57374068 (推荐一看)
java NIO - DirectBuffer 和 HeapBuffer的更多相关文章
- 3、谈谈 Java NIO
在 JDK1.4 之后,为了提高 Java IO 的效率,Java 提供了一套 New IO (NIO),之所以称之为 New,原因在于它相对于之前的 IO 类库是新增的.此外,旧的 IO 类库提供的 ...
- Java NIO浅析 转至 美团技术团队
出处: Java NIO浅析 NIO(Non-blocking I/O,在Java领域,也称为New I/O),是一种同步非阻塞的I/O模型,也是I/O多路复用的基础,已经被越来越多地应用到大型应用服 ...
- Java NIO 基础
Java在JDK1.4中引入了 java.nio 类库,为Java进军后端Server和中间件开发打开了方便之门. 一般而言,这里的 nio 代表的是 New I/O,但是从实质上来说,我们可以将其理 ...
- JAVA NIO复习笔记
1. JAVA NIO是什么? 从JDK1.4开始,java提供了一系列改进的输入/输出处理的新功能,这些功能被统称为新IO(New IO,简称NIO),新增了许多用于处理输入/输出的类,这些类都被放 ...
- Java NIO 学习笔记
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3344148.html ...
- Java NIO 之 Buffer
Java NIO 之 Buffer Java NIO (Non Blocking IO 或者 New IO)是一种非阻塞IO的实现.NIO通过Channel.Buffer.Selector几个组件的协 ...
- 详解 Java NIO
文件的抽象化表示,字节流以及字符流的文件操作等属于传统 IO 的相关内容,我们已经在前面的文章进行了较为深刻的学习了. 但是传统的 IO 流还是有很多缺陷的,尤其它的阻塞性加上磁盘读写本来就慢,会导致 ...
- 从I/O多路复用到Netty,还要跨过Java NIO包
本文是Netty系列第4篇 上一篇文章我们深入了解了I/O多路复用的三种实现形式,select/poll/epoll. 那Netty是使用哪种实现的I/O多路复用呢?这个问题,得从Java NIO包说 ...
- 源码分析netty服务器创建过程vs java nio服务器创建
1.Java NIO服务端创建 首先,我们通过一个时序图来看下如何创建一个NIO服务端并启动监听,接收多个客户端的连接,进行消息的异步读写. 示例代码(参考文献[2]): import java.io ...
随机推荐
- Visual detection of structural changes in time-varying graphs using persistent homology
PKU blog about this paper Basic knowledge: 1. what is time-varying graphs? time-varying graph VS st ...
- servlet中doGet()和doPost()的用法
转自:https://blog.csdn.net/qq_38963960/article/details/79468182 1.servlet中doGet()和doPost()的用法 一般来说我们是用 ...
- codeforces 1284E. New Year and Castle Construction(极角排序+扫描枚举)
链接:https://codeforces.com/problemset/problem/1284/E 题意:平面上有n个点,问你存在多少组四个点围成的四边形 严格包围某个点P的情况.不存在三点共线. ...
- 随缘记录 LeetCode第168场周赛 2019-12-22
5292. 划分数组为连续数字的集合 给你一个整数数组 nums 和一个正整数 k,请你判断是否可以把这个数组划分成一些由 k 个连续数字组成的集合. 如果可以,请返回 True:否则,返回 Fals ...
- cmd命令和快捷键
命令 含义 盘名: 从C盘到其他盘时输入 dir 查看当前文件夹下的内容 cd 目录名称 选择目录 cd.. 退回当前目录的上一级目录 cd\ 回到根目录 md 目录名称 创建目录 rd 目录名称 删 ...
- 题解【洛谷P1046】[NOIP2005普及组] 陶陶摘苹果
[NOIP2005] 陶陶摘苹果 首先,我们用一个数组s[11]存储每个苹果的高度. 然后,用a表示陶陶的身高. 接着,用a+30与s[i]比较,大于则计数器加一. 最后,输出计数器的值即可. #in ...
- 项目中的process.bpmn的读-过程
1.这次项目中遇到了process.bpmn类的封装好的类.怎么读呢?不知道,一周过去了,总算明白点. 2.首先也是从Controller开始,走进service层,比如mybatis,调用的就不是m ...
- C语言究竟是一门怎样的语言?
对于大部分程序员,C语言是学习编程的第一门语言,很少有不了解C的程序员. C语言除了能让你了解编程的相关概念,带你走进编程的大门,还能让你明白程序的运行原理,比如,计算机的各个部件是如何交互的,程序在 ...
- 动态设置微信小程序 navigationBarTitle 的值
wx.setNavigationBarTitle({ title:' 动态值 ' })
- my codestyle
代码风格 缩进 缩进采用4个空格或tab. 原则是:如果地位相等,则不需要缩进:如果属于某一个代码的内部代码就需要缩进. 变量命名 变量命名遵守遵从驼峰命名法,统一使用lowerCamelCase风格 ...