FileChannel transferFrom's comments explanation

本秂侑毒 提交于 2019-12-11 09:28:44

问题


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:


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.



来源:https://stackoverflow.com/questions/17310565/filechannel-transferfroms-comments-explanation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!