I am reading the Linux Progamming Interface.
49.9 MAP_NORESERVE and Swap Space Overcommitting
Some applications create large (usually private anonymous) mappings, but use only a small part of the mapped region. For example, certain types of scientific applications allocate a very large array, but operate on only a few widely separated elements of the array (a so-called sparse array).
If the kernel always allocated (or reserved) enough swap space for the whole of such mappings, then a lot of swap space would potentially be wasted. Instead, the kernel can reserve swap space for the pages of a mapping only as they are actually required (i.e., when the application accesses a page). This approach is called lazy swap reservation, and has the advantage that the total virtual memory used by applications can exceed the total size of RAM plus swap space.
To put things another way, lazy swap reservation allows swap space to be overcommitted. This works fine, as long as all processes don’t attempt to access the entire range of their mappings. ...
As far as I know, swap space is a chunk of space in disk, reserved for memory swapping. When those pages in memory are inactive, they are swapped into swap space in disk. It's like a second level cache for memory/ram.
Then what the hell is this lazy swap reservation mechanism?
Let me demostrate my confusion with an example.
Some applications create large (usually private anonymous) mappings....
Ok, then assume I malloc
a big array 16384(4096*4)
bytes (create large (usually private anonymous) mappings), and operate on only a few widely separated elements of the array.
Then some inactive pages are swapped into swap space, right? Let's say 0-4095(4096B)
, 8192-12287(4096B)
are in memory, and all the other inactive pages, 4096-8191(4096B)
, 12288-16383(4096B)
are swapped into swap space.
Then what does it mean by saying:
Instead, the kernel can reserve swap space for the pages of a mapping only as they are actually required (i.e., when the application accesses a page).
Where else can these inactive pages (4096-8191(4096B)
and 12288-16383(4096B)
) stay in, if not staying in the swap space? The text seems to indicate that there is a 3rd level cache for swap space.
memory -> swap space (disk) -> ????