javaniomemory-mapped-filesmappedbytebuffer

MappedByteBuffer - mapping of pages into physical memory


As far as I see it, the MappedByteBuffer, after a call to FileChannel.map "maps" file content into memory, but not necessarily loading the whole file in. So, if I start reading e.g. at pos(0), a page might be actually loaded from disk at that time e.g. first 4096 bytes of the file, into the main memory (outside of Heap, as it is a direct memory buffer).

My question is - what happens if I call load() method on the buffer? would that, at best effoert, try to map the ENTIRE file into memory e.g. exactly load 1GB of data, considering this is the amount I mapped?


Solution

  • Would it try to map the ENTIRE file into memory e.g. exactly load 1GB of data, considering this is the amount I mapped?

    Basically yes. The JVM will try to make that happen.

    But "best effort" means that that it is not guaranteed that the entire file will be loaded. And there is no guarantee that the file will stay loaded.

    The behaviour is ultimately determined by the operating system (which manages virtual memory resources), and it will depend on how much physical memory there is on the system, what other applications need, and potentially on other things.