title: Why mmap() (Memory Mapped File) is faster than read()
body: <p>I was working on sth about MappedByteBuffer of Java NIO recently.
  I've read some posts about it and all of them mention that "mmap() is faster than read()"</p>
  
  <p>In my conclusion:</p>
  
  <ol>
  <li><p>I treat MappedByteBuffer == Memory Mapped File == mmap()</p></li>
  <li><p>read() has to read data through : disk file -> kernel -> application, so it has context switch and buffer copying</p></li>
  <li><p>They all said mmap() has less copying or syscall than read(), but as I know it also need to read from disk file the first time you access the file data. So the first time it read : virtual address -> memory -> page fault -> disk file -> kernel -> memory. Except you can access it randomly, the last 3 steps (disk file -> kernel -> memory) is exactly the same as read(), so how mmap() could be less copying or syscall than read() ?</p></li>
  <li><p>what's the relationship between mmap() and swap file, Is that the os will put the least used file data of memory into swap (LRU) ? So when the second time you access these data, OS retrieves them from swap but not disk file(no need to copy to kernel buffer), that's why mmap() has less copying and syscall ?</p></li>
  <li><p>In java, MappedByteBuffer is allocated out of heap (it's a direct buffer). So when you read from MappedByteBuffer, does it mean it need one more extra memory copy from outside the java heap into java heap?</p></li>
  </ol>
  
  <p>Could anyone answer my questions ? Thanks :)</p>