First of all, let me say that I very well know how VirtualAlloc()
works and how virtual memory facilities of the modern operating systems, in general, work.
Said that, using the MAP_RESERVE
flag of the VirtualAlloc()
Windows API, without using also MEM_COMMIT
, has some practical use today?
I mean, when I call VirtualAlloc()
with MEM_RESERVE|MEM_COMMMIT
I both reserve and commit pages, but I know that the operating system will really allocate pages only when I try to write into them.
This optimization happens on practically all the modern operating systems.
So, with this optimization in mind, if I call VirtualAlloc()
with MEM_RESERVE
and then I call it several times with MEM_COMMIT
to commit pages, isn't the same result of calling VirtualAlloc()
only once, specifying MEM_RESERVE|MEM_COMMIT
?
Since specifying only MEM_RESERVE
will only reserve pages boundary addresses, without committing real pages, but then MEM_RESERVE|MEM_COMMIT
will reserve+commit only pages I write to, isn't using MEM_RESERVE
alone, a waste of time today?
With only 1 call to VirtualAlloc()
with MEM_RESERVE|MEM_COMMIT
I can basically obtain the same result of calling VirtualAlloc()
1 time with MEM_RESERVE
and calling it several N times with MEM_COMMIT
.
As a proof of what I am saying, I have noticed that the MEM_RESERVE
facility doesn't exist at all in Unices/POSIX systems which use the mmap(2)
syscall.
Also there you can "commit" a big chunk of pages calling mmap(2)
once, and then the pages will be really allocated only when you write into them, all optimized by the operating system.
So, is using MEM_RESERVE
alone just an old days thing useful only when memory pages were a precious resource, and so today is useless?
Or, using this flag alone (and then calling VirtualAlloc()
N times with MEM_COMMIT
) still has some practical use that I am missing?