May 28, 2001 (Computerworld) Until recently, an explanation of memory management would have amounted to a description of a computer's virtual memory implementation. Now however, memory management encompasses organizing frequently used resources in memory to boost overall system performance.
Virtual memory is a sleight-of-hardware that makes a computer appear to have more physical memory than it does. This is accomplished by swapping unused resources out of physical memory and replacing them with those required to execute the current operation. The software that orchestrates this process is known as the memory manager or memory management unit (MMU).
If the CPU supports virtual memory, the operating system doles out different groups of addresses (called address spaces) to operating system components and any executing applications. These virtual address spaces don't need to match the addresses of the computer's physical RAM. The MMU then maps active blocks of the virtual address spaces into the available physical memory. The smallest block of memory the processor can manage this way is called a page and is typically 4KB.
The CPU maintains page tables that track how the virtual addresses map into physical memory. These tables also manage critical bookkeeping functions such as determining whether a page is associated with a physical memory page and, if so, when it was last accessed. If a page is present in physical memory, the CPU uses the page tables to rapidly translate access to a virtual address into a physical address.
The MMU typically uses demand paging to implement virtual memory; that is, it only swings into action when an application demands a resource (perhaps a function call to a shared library, or a spreadsheet reading in more numbers) that isn't in physical memory. This demand is detected when the operating system or application attempts to access a page that the CPU determines (via its page tables) isn't in physical memory. The CPU generates a memory exception, which is handed over to the MMU.
The MMU uses the page table's information to locate an unused physical memory page (one that hasn't been accessed recently, for example). It writes the unused page out to a reserved area of disk called the swap file. The MMU then has the CPU read the requested page into physical memory, from either a file on disk or the swap file. As this is done, the MMU maps the virtual and physical pages and updates the page tables.
Using virtual memory, the computer seems to have more memory than it does, within limits. With adequate physical memory, the MMU isn't called often, and the computer spends most of its time executing applications. With too little memory, the computer spends most of its time moving pages between memory and the swap file, a performance-sapping phenomenon called thrashing. A good rule of thumb is that virtual memory shouldn't be more than 1.5 times physical memory.
Moving pages to and from the swap file is slow, since a hard drive access is more than 1,000 times slower than a memory access. This can slow the overall performance of the computer with virtual memory active. There are ways to improve the situation, however.
One such technique is file mapping, where files on the computer's hard drive are mapped to virtual memory pages. Now, when the MMU determines that it has to swap out pages that contain application code, it does nothing - the MMU reads the required resources into physical memory, and the code pages are discarded.
The reason for discarding the code pages is that it isn't necessary to write nonvolatile code pages to the swap file. In addition, the code pages are easily recovered by reading the original file when the MMU needs them. In other words, if the MMU has to read in code pages, it might as well read them from the original file, rather than write and then read them from the swap file.
File mapping makes it possible to reduce the number of hard-disk accesses required to maintain memory resources, since the MMU now only writes modified data pages to the swap file.
Another way to boost performance is to use a virtual cache. A virtual cache uses a portion of physical memory to store code and data that the operating system might use frequently. Since the operating system is responsible for loading applications and data files into memory, it can accumulate this type of tracking information for use with the virtual cache. The advantage of this scheme is that rather than reading the hard drive frequently for a heavily-used resource, the MMU can fetch it from memory.
Both file mapping and the virtual cache let the MMU work smarter rather than harder, thereby improving the system's overall performance.
Thompson is a training specialist at Metrowerks Inc. in Hollis, N.H. You can contact him at thompson@metrowerks.com.