c++linuxmemory-managementcuda

Virtual memory management in Linux


I recently learned about CUDA's low-level GPU virtual memory management, as described in this blog post. The key idea is:

Reserving a large chunk of virtual memory without initially allocating physical memory. Dynamically mapping physical memory to the reserved virtual memory when needed. Unmapping and releasing physical memory once it is no longer required. This technique is particularly useful for data structures that need to grow dynamically, such as vectors.

My question is: Can this approach be implemented using Linux and C++? AFAIK, there isn't a Linux API that allows reserving virtual memory without backing it with physical memory upfront. Is there a way to achieve this functionality on Linux?


Solution

  • It's the default behaviour on Linux. Whenever you allocate memory in your C++ application you get a virtual address back and the kernel will not initially back that with physical memory. Only when a page fault occurs (for example because you write to the memory) will the kernel map some physical memory to back the allocation (but even then it'll usually only physically allocate the pages you wrote to, not the entire allocation).

    That is, by default the kernel overcommits memory though that behaviour can be changed.