[NIO] MappedByteBuffer- memory-mapped file I / O

The operating system is responsible for the implementation of the mapping operation for large files

java io operation usually BufferedReader, BufferedInputStream other IO classes buffered large files; java nio introduced MappedByteBuffer operation mode based on large files, read and write high performance

 

FileChannel provides a map method to map the file into virtual memory, usually can map the entire file, if the file is large, it can be segmented map

MBB = fc.map a MappedByteBuffer (FileChannel.MapMode.READ_WRITE, 0, 1024); FC // -> FileChannel 
// Map map0 the mapping file by native work function, returns DirectByteBuffer

  

 MappedByteBuffer get methods to achieve the ultimate method by DirectByteBuffer.get

public byte get() {
    return ((unsafe.getByte(ix(nextGetIndex()))));
}
public byte get(int i) {
    return ((unsafe.getByte(ix(checkIndex(i)))));
}
private long ix(int i) {
    return address + (i << 0);
}

  

map0 () function returns an address address, so there is no need to call a method to read or write the file read and write, it is possible to operate by the address file. The method using the underlying unsafe.getByte, gets the specified memory data (address + offset).

  1. First visit to address at the memory area, leading to a page fault interrupt, the interrupt function to find the corresponding page in the swap area, if not found (that is, the case file had never been read into memory), the designated page read from a file on the hard disk physical memory (non jvm heap memory) .
  2. If when copying data, found that the physical memory is not enough, it will be switched temporarily without physical page to the hard disk virtual memory virtual memory mechanism (swap) in

 

performance:

From the code level, the file from the hard disk will be read into memory, go through the file system to copy data, and the data copy operation is performed by the file system and hardware drivers to achieve, in theory, efficiency copy of the data is the same.
But through memory-mapped files on the method of accessing the hard disk, high-efficiency than the read and write system calls, which is why?

  • read () system call, the file is first copied from the hard disk to a buffer in the kernel space, and then copy the data to the user space, data copying is actually performed twice;
  • map () system call also, but not to copy data when a page fault occurs, the files directly from the hard copy to user space, only a copy of the data.

Therefore, the use of memory-mapped read efficiency of the conventional high read / write performance than the

to sum up:

    • MappedByteBuffer use of virtual memory , so allocation (map) of memory size is not the JVM -Xmx parameter limits, but also a size limit.
    • If, when the file exceeds the limits 1.5G, contents of the back position parameter file can be re-map.
    • MappedByteBuffer indeed very high performance when dealing with large files, but there are some problems, such as memory footprint, the file is closed uncertainty, it is only open file is closed garbage collection will, and this point in time is uncertain .
      javadoc also mentioned: A mapped byte Buffer and Mapping The File that Represents by IT REMAIN Valid an until Buffer Itself The IS-Garbage Collected. *

Guess you like

Origin www.cnblogs.com/itplay/p/11078559.html