FileChannel transferFrom's comments explanatio

2019-07-23 18:31发布

I've read the comment on FileChannel's transferFrom

 * <p> This method is potentially much more efficient than a simple loop
 * that reads from the source channel and writes to this channel.  Many
 * operating systems can transfer bytes directly from the source channel
 * into the filesystem cache without actually copying them.  </p>

What does it mean by

Many operating systems can transfer bytes directly from the source channel
into the filesystem cache without actually copying them.

If I read from a Channel and then write it to another channel, doesn't it transfer bytes into cache ?

1条回答
聊天终结者
2楼-- · 2019-07-23 18:45

Yes, if you use a loop and read from the source channel into a ByteBuffer and then write the ByteBuffer to the FileChannel the bytes/data will be in the file system cache at the end of the write. They will have also been copied into the Java ByteBuffer and that might have been a copy from the kernel, to application memory (or the "C heap") and then into the JVM heap (in the worst case).

If the source channel is compatible then the OS may be able to avoid the copy into the JVM heap and maybe even out of the kernel entirely and instead do the copy directly from, say, the source file page into the destination file page.

If you will see any real improvement in performance will be highly JVM version, OS and file system dependent. I would not expect it to ever perform worse that a Java coded loop.

Rob.

查看更多
登录 后发表回答