简体   繁体   English

了解java.nio包中的通道

[英]understanding channel in java.nio package

For direct mapped buffer, they always stay outside jvm heap. 对于直接映射的缓冲区,它们始终位于jvm堆之外。 Channels, on the other hand, seem to also exists in the IO operation. 另一方面,IO操作中似乎也存在通道。 I'm just wondering if a the corresponding channel for the direct mapped buffer also stays outside jvm heap? 我只是想知道直接映射缓冲区的相应通道是否也保留在jvm堆之外?

Also, The other question comes from the necessity of existence of Channel in terms of memory operation efficiency. 另外,另一个问题来自在存储操作效率方面是否存在Channel的必要性。 I understand that 我明白那个

A channel represents an open connection to an entity such as a hardware device 通道表示与实体(例如硬件设备)的开放连接

Yet, 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? 与“直接”写入IO设备相比,效率低吗?

I'm just wondering if a the corresponding channel for the direct mapped buffer also stays outside jvm heap? 我只是想知道直接映射缓冲区的相应通道是否也保留在jvm堆之外?

The question doesn't make sense. 这个问题没有道理。 A Channel isn't a piece of memory, it is an interface to an operating system FD. 通道不是内存,它是操作系统FD的接口。

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? 与“直接”写入IO设备相比,效率低吗?

No. The MappedByteBuffer is independent of the channel it came from. MappedByteBuffer与它来自的通道无关。 For example, it isn't closed when the channel is closed. 例如,当通道关闭时,它不会关闭。

Are you perhaps looking for direct byte buffers? 您是否正在寻找直接字节缓冲区? They do exist, and you write to them via channels, but I/O via them happens once, not twice. 它们确实存在,并且您通过通道写入它们,但是通过它们的I / O发生一次,而不是两次。

I think I figure out the answer. 我想我想出了答案。

Direct mapped buffer first write to a buffer, which is non IO operation. 直接映射的缓冲区首先写入缓冲区,这是非IO操作。 The IO operation for real write that updates the file happens afterwards. 随后,将执行用于更新文件的实际写入的IO操作。 The reason for two write is that since we have DMA, we don't want all IO operation happen by times. 进行两次写入的原因是,由于我们具有DMA,因此我们不希望所有IO操作都偶尔发生。 All IO operation in one time is more efficient. 一次执行所有IO操作效率更高。

Getting the starting address through the JNI is not correct address for accessing that piece of memory. 通过JNI获取起始地址不是访问该内存的正确地址。 Instead, the address for data is a field is hidden in java.nio.buffer. 相反,数据地址是一个字段,隐藏在java.nio.buffer中。 One way to get this field is though sun.misc.Unsafe in making the field public. 获得此字段的一种方法是通过sun.misc.Unsafe将该字段公开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM