java - 了解java.nio包中的 channel

标签 java io buffer nio

对于直接映射缓冲区,它们始终位于 jvm 堆之外。另一方面, channel 似乎也存在于 IO 操作中。我只是想知道直接映射缓冲区的相应 channel 是否也位于 jvm 堆之外?

另外,另一个问题来自于Channel存在的必要性,从内存操作效率的角度来看。我明白了

A channel represents an open connection to an entity such as a hardware device

但是,在通过直接映射缓冲区写入文件的情况下,内容是否会被写入两次?内容首先写入缓冲区,然后写入 channel 。与“直接”写入 IO 设备相比,这会降低效率吗?

最佳答案

I'm just wondering if a the corresponding channel for the direct mapped buffer also stays outside jvm heap?

这个问题没有意义。 Channel 不是一 block 内存,它是操作系统 FD 的接口(interface)。

in the situation of writing to file through the direct mapped buffer, is the contents being written twice? The content is first written to the buffer then to the channel. Would this be low efficiency versus "directly" writing to the IO device?

没有。 MappedByteBuffer 与其来自的 channel 无关。例如,当 channel 关闭时,它并没有关闭。

您是否正在寻找直接字节缓冲区?它们确实存在,您可以通过 channel 向它们写入数据,但通过它们进行的 I/O 只发生一次,而不是两次。

关于java - 了解java.nio包中的 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25941794/

相关文章:

java - 获取相机和精确物体之间的距离

java - 高效查找特定目录下的文件

Vim - 当移动到具有相同缓冲区的其他窗口时调用缓冲区事件

haskell - 在 Haskell 中读取和处理多个文件

java - ObjectOutputStream 人类可读

java - 缓冲短 I/O

buffer - FreeBSD:有关 NIC 环形缓冲区、mbuf 和 bpf 缓冲区的问题

java - 即使没有屏幕旋转,也要让 ViewModel 保持 Activity 状态

java - 内存不足(堆)问题无法修复

c++ - 大量文件的原子删除