Reading a file in using a Mapped FileChannel
seems to be lightning fast... But I was wondering how they are doing this?
Are they simply just reading in a large (~64kB) buffer and then letting me march through that? Or is there more to it?
I'm just impressed by the speed and want to better understand the algorithm behind it.
They don't read anything until you do, then the piece you read is basically read via the OS paging system. The open may cost you almost nothing but repeated reads of the same piece of the file may cause repeated I/O. Nothing is free.
memory mapping, maps the file into your memory and Java provides a library to to wrap this so you can access it relatively safely.
It's benefits include:
It's disadvantages include:
I have written a couple of libraries making memory mapped files easier to work with and I would say I would use it when ultra-low latency is critical or you need to read large amounts of memory which you expect to be in disk cache already and you want to make the most of your disk cache.
It's worth noting that memory mapped doesn't make your disk sub-system faster and if that is your limiting factor it won't matter which way you read/write data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.