Each virtual address that you are trying to obtain is mapped by the operating system to physical memory. Physical memory is allocated on pages (for example, 4 KB). If you manage to select a byte at an offset of 1,000,000 * n and do it for n from 1 to 1,000,000 (I think you could do it with mmap), then the OS will have to return this with a million pages of physical memory, which is something like 4G . This physical memory will not be available for anything else. If you allocated bytes contiguously, you only need about 1M of physical memory (256 pages) for your million bytes.
You may encounter similarly bad situations if you assigned 4G for legitimate reasons, and then free some of it, saving a little of each page. The OS will not be able to really reuse the freed memory for anything else, because there are no completely free physical pages. So the problem is fragmentation.
In theory, you could imagine that the virtual addresses 1,000,000 and 2,000,000 would be mapped onto the same page of physical memory, avoiding fragmentation. But in practice and for good reasons, virtual memory mapping is done page by page. You can read more about this here: http://en.wikipedia.org/wiki/Page_table .
Ds.
source share