我读过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>这是什么意思?
Many operating systems can transfer bytes directly from the source channel
into the filesystem cache without actually copying them.如果我从一个通道读取,然后将其写入另一个通道,它不会将字节传输到缓存中吗?
发布于 2013-06-26 10:43:16
是的,如果您使用循环并从源通道读取到ByteBuffer,然后将ByteBuffer写入FileChannel,则在写入结束时,字节/数据将位于文件系统缓存中。它们还将被复制到Java中,这可能是从内核复制到应用程序内存(或"C堆“),然后再复制到ByteBuffer堆(在最坏的情况下)。
如果源通道是兼容的,则OS可能能够避免复制到JVM堆中,甚至可能完全从内核之外,而是直接从比方说源文件页复制到目标文件页中。
如果您将看到任何真正的性能改进,将高度依赖于JVM版本、操作系统和文件系统。我认为它的性能不会比Java代码的循环差。
罗伯。
https://stackoverflow.com/questions/17310565
复制相似问题