Virtual memory

October 29, 2012

Memory is a precious resource that is managed by the operating system. There are several sorts of memory in the standard computing platform: disk, registers, and RAM memory. The discussion of virtual memory is about the management of RAM memory, which we will call physical memory, and sometimes we will talk about the disk, for example, as the source of data for physical memory, or for swap space (see later).

A modern computing architecture supports the technique of virtual memory, a way of decoupling the physical memory from an abstract address space, called virtual memory. The instructions in a program will address memory in the virtual memory space, using virtual addresses. It is the operating system’s task to allocate appropriate physical memory so that when the program attempts to use a virtual address, there is actual physical memory behind that address, implementing the storing or retrieving of the data.

There are many motivations for the invention of virtual memory, and once implemented, many more memory management “tricks” are enabled by virtual memory.

Since there are several processes sharing the physical memory, there needs to be some system in place for the processes to coordinate their use of physical memory. Processes have to claim various pieces of memory exclusively, and their instructions must only refer to memory they have claimed. For a reliable computing system, the hardware enforces that a process only manipulates the physical memory that is has claimed. These are requirements for an reliable multi-processing system, and virtual memory provides a mechanism to meet these requirements.

Virtual memory gives each process a clean and dedicated memory space of a numbered series of bytes. The address of the bytes starts at 0 and goes up to some number fixed by the CPU’s architecture: 232 – 1 for 32 bit architectures, and 264 – 1 for 64 bit architectures. These virtual addresses are not the same as the addresses used when the physical memory is accessed to store or extract data from the RAM data chips. This is the idea of virtual memory — those addresses are different, and the mapping between the two spaces is maintained by the operating system automatically as a service to the user program.

Since virtual addresses and physical addresses need not be the same, two processes can use memory at the same virtual address without interference. Each process will have that virtual address mapped to a different physical address. Because a process can only access physical memory through operating system managed mappings from its virtual space, there are strong protection guarantees. A process cannot affect another process’s memory because it cannot get possession of any physical address without the operating system’s intervention.

Therefore virtual memory provides protection and simplicity in the allocation of physical memory.

It also enables a more efficient use of memory. As a precious resource, many processes will be making demands for physical memory. It is inefficient to simply allocate to each process physical memory for the entire virtual space. The virtual space is made very large mostly for convenience, and most processes will use only a small fraction of that space. In a certain time period, a processes uses an even a smaller fraction of the entire virtual address space.

On a standard 32 bit platform, both the virtual and physical spaces have 32 bit addresses, for 4 Gigabyte (approx.) of possible space. If the operating system were to completely back virtual memory with physical memory, the first process would take up all the physical memory available, and the system would have to halt. On 64 bit platforms, it just so happens that physical memory can exceed virtual memory, but the virtual memory spaces are so huge that few programs can justifiably use more than the smallest fraction of the virtual space.

Virtual memory allows the operating system to use a “just in time” protocol to allocate physical memory to processes. Regions of the virtual memory space which have been used, or will be used, but are not in use at the moment, can have the data reside on slower, larger memories, namely, the disk. If the data is a read-only sort, for instance, the program instructions, that data is already on the disk. In this situation, it is a sort of just-in-time reading of instructions. Regions of virtual memory that are never used will receive no physical memory backing store. This is called demand paged virtual memory.

In cases of extreme memory pressure, associations made between virtual and physical address must be dropped, and the physical memory reassigned to another virtual address in a possibly different process. The virtual memory management system in the kernel will make a guess as to what regions of a processes’ virtual address space has “gone cold”, that is, that will not be referenced in the near future. The physical memory backing these virtual addresses are candidates for reassignment. This is called page eviction.

The the evicted section contains data that was originally brought in from, say, a file, and has not changed, nothing more need be done to reused the physical memory than to break its current association to virtual memory, zero out the physical memory to avoid an information leak, and re-associate it to the new virtual address space. If this virtual address range is needed again in the future, the data can be brought in again from the file.

If the section contains data that was completely or partially created during the process run, then its these changes cannot be discarded, since the memory management system cannot be sure that the data will not be needed in the future. In this case, the data is copied out to swap space, a sort of cold storage for data, and then the physical page is released for reuse. If and when this range of virtual address as again in demand, the physical memory contents will be restored from the copy of the data in swap space, and associated again to the processes’ virtual address space.

Virtual memory therefore gives efficient use of memory, never assigning memory to virtual addresses which are never used, and moving the contents of virtual addresses which are not currently being used to a more spacious but slower form of storage.

posted in CSC521 by admin

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org