March 18th, 2016

What are the consequences of reserving a huge amount of memory, most of which is never committed?

A customer had an idea of solving a program by reserving a huge amount of memory with Virtual­Alloc, and then committing only the parts that are actually needed. And by huge, they’re talking about 100GB. (Obviously, on a 64-bit machine.) Is this okay, or is it a bad idea, or is it a terrible idea?

Starting in Windows 8.1 and Windows Server 2012 R2, it’s cheap to reserve a lot of virtual address space. No major memory allocation or other charges occur until you start committing pages, in which case the memory requirements are proportional to the amount of address space you commit, rather than the amount of address space you reserve.

Things were different in earlier versions of Windows. Back then, reserving address space still incurs a cost for creating the page tables required to map the reserved region. In the above example, a 100GB reservation would cost 200MB of memory for the page tables. Depending on the nature of the system you intend to run the program on, this may be an acceptable cost.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.