2 ms·
For example, you create a 100MB file. You tell the operating system to map that file to memory, and it gives you a pointer to 100MB of memory. Whatever you read
by Salgat 4y ago
For example, you create a 100MB file. You tell the operating system to map that file to memory, and it gives you a pointer to 100MB of memory. Whatever you read from that 100MB of memory is what is actually in the file. You can also write to that memory and commit it back to the file. The parts you read from the pointer are the only parts from the file that are loaded into memory. So if you memory map a 100GB file, the operating system won't actually load all 100GB into memory, only what is accessed (and this is all handled for you automatically).
The operating system is free to load and cache the file into memory in whatever way it wants, so for large memory mapped files it'll often try to use all available memory to cache as much as possible. If another program needs memory, the operating system will simply lower the amount of memory available to the memory mapped file for caching. This is extremely useful for databases, since it greatly simplifies both how to persist the data along with how to load and cache the persisted data.
This all comes with a big caveat however. The less memory you have, the more file accesses occur (similar to your pagefile when you're thrashing), which can dramatically slow down your memory operations.
tldr; it lets you designate a file to use as a region of memory.
- Jasper_ 4y agoThe kernel page cache already exists, and is used on read() too. The distinction is that instead of read() copying from the kernel page cache, mmap will map that page cache page directly into the user's memory address space. It saves a copy on read, in exchange for a bit more overhead configuring the processes address space up front.
- chasd00 4y agoso basically an MRU cache for a file's contents? Is there enough information to know what bytes from the file are cache'd and which are not? It would interesting to see like a heatmap of a large model showing what portions of the neural network are being used the most. ..like a brain MRI more or less.