Using Virtual Memory to Store Data
Running applications use what is called virtual memory addresses to hold data. The data is still held in memory, but rather than the application storing the exact location in the memory chips where the data is held, the application uses a virtual address, which then gets translated into the actual address in physical memory. Figure 1.16 shows schemati-cally the process of translating from virtual to physical memory.
This sounds like an unnecessarily complex way of using memory, but it does have some very significant benefits.
The original aim of virtual memory was to enable a processor to address a larger range of memory than it had physical memory attached to the system; at that point in time, physical memory was prohibitively expensive. The way it would work was that memory was allocated in pages, and each page could either be in physical memory or be stored on disk. When an address was accessed that was not in physical memory, the machine would write a page containing data that hadn’t been used in a while to disk and then fetch the data that was needed into the physical memory that had just been freed. The same page of physical memory was therefore used to hold different pages of virtual memory.
Now, paging data to and from disk is not a fast thing to do, but it allowed an applica-tion to continue running on a system that had exhausted its supply of free physical memory.
There are other uses for paging from disk. One particularly useful feature is accessing files. The entire file can be mapped into memory—a range of virtual memory addresses can be reserved for it—but the individual pages in that file need only be read from disk when they are actually touched. In this case, the application is using the minimal amount of physical memory to hold a potentially much larger data set.
The other advantage to using virtual memory is that the same address can be reused by multiple applications. For example, assume that all applications are started by calling code at 0x10000. If we had only physical memory addresses, then only one application could reside at 0x10000, so we could run only a single application at a time. However, given virtual memory addressing, we can put as many applications as we need at the same virtual address and have this virtual address map to different physical addresses. So, to take the example of starting an application by calling 0x10000, all the applications could use this same virtual address, but for each application, this would correspond to a different physical address.
What is interesting about the earlier motivators for virtual memory is that they become even more important as the virtual CPU count increases. A system that has many active threads will have some applications that reserve lots of memory but make little actual use of that memory. Without virtual memory, this reservation of memory would stop other applications from attaining the memory size that they need. It is also much easier to produce a system that runs multiple applications if those applications do not need to be arranged into the one physical address space. Hence, virtual memory is almost a necessity for any system that can simultaneously run multiple threads.