Data Storage - Virtual memory

1 - History (Why virtual)

The memory management subsystem is one of the most important parts of the operating system. Since the early days of computing, there has been a need for more memory than exists physically in a system.

In fact, there is really only one fact that system administrators should always keep in mind: There is never enough RAM

Strategies have been developed to overcome this limitation and the most successful of these is virtual memory. Virtual memory is a way of combining RAM modules with slower storage (such as disk storage drives) to give the system the appearance of having more RAM modules than is actually installed.

Disk I/O speeds are about 10-100 times slower than memory. Disk I/O speeds will be very fast when data is store on filer disk arrays because such devices usually have a large amount of memory to cache data.

2 - Process

From the operating system perspective, an application is a process or a collection of separate processes. The processes reserve memory for their use by requesting an allocation from the operating system. The entire reserved memory footprint of a program is referred to as its Virtual Memory.

Each process has its own virtual memory, which grows when the process requests more memory from the operating system, and shrinks when the process relinquishes unused memory. You can think of virtual memory size as the memory amount that the process has requested (or allocated) from the operating system, including reservations for its code, stack, data, and memory pools (under program control).

When part of the virtually allocated memory actually needs to be used, it is loaded or mapped to the real, physical memory of the host and becomes resident.

Over time, the operating system may swap out some of a process’ resident memory, according to a least-recently-used algorithm, to make room for other code or data. Thus, a process’ resident memory size may fluctuate independently of its virtual memory size. In a properly sized host there is enough physical memory, and thus swapping is disabled and should not be observed.

4 - Type

Conceptually speaking, there are only three types of memory:

  1. Kernel memory: memory needed by the OS to manage itself
  2. Private memory: memory used by user programs
  3. Shared memory: memory that can be used by multiple programs simultaneously

There are many types of memory statistics available. They are all called differently depending on the OS and the type of monitoring tools. Reading and understanding is a matter of figuring out which of the three types is shown where. The biggest confusion comes from counting the same memory multiple times.

5 - Virtual Memory Concept

5.1 - Address Space

The address space is the number of unique addresses needed to hold both:

  • the application
  • and its data.

Example:

Application Memory Needs Size (bytes)
Application Code 10000
Data storage and I/O buffers 5000
Total to be able to run 15000

This 15000 byte requirement is known as the application's address space.

In the first computers, the amount of available RAM had to be greater than the address space of the largest application to be run; otherwise, the application would fail with an “out of memory” error.

5.2 - Virtual address space

The word “virtual” means that this is the total number of uniquely-addressable memory locations required by the application, and not the amount of physical memory that must be dedicated to the application at any given time.

In the case of the above example application, the virtual address space is 15000 bytes.

5.3 - Memory Management Unit

In order to implement virtual memory, it is necessary for the computer system to have special memory management hardware. This hardware is often known as an MMU (Memory Management Unit). Without an MMU, when the CPU accesses RAM, the actual RAM locations never change — memory address 123 is always the same physical location within RAM.

5.4 - Page

However, with an MMU, memory addresses go through a translation step prior to each memory access. This means that memory address 123 might be directed to physical address 82043 at one time, and physical address 20468 another time. As it turns out, the overhead of individually tracking the virtual to physical translations for billions of bytes of memory would be too great. Instead, the MMU divides RAM into pages which are contiguous sections of memory of a set size that are handled by the MMU as single entities.

5.5 - Page table (virtual to physical addresses)

6 - Virtual Memory Benefits

Virtual memory does more than just make your computer's memory go further. The memory management subsystem provides:

  • Large Address Spaces with Swapping

The operating system makes the system appear as if it has a larger amount of memory than it actually has. The virtual memory can be many times larger than the physical memory in the system,

  • Protection with the virtual address space

Each process in the system has its own virtual address space. These virtual address spaces are completely separate from each other and so a process running one application cannot affect another. Also, the hardware virtual memory mechanisms allow areas of memory to be protected against writing. This protects code and data from being overwritten by rogue applications. Protected Mode is the virtual memory protection ? : Modern operating systems run processes in “protected mode”. What this mean is that a process can only “see” its own memory. It cannot interfere with other processes – memory or with kernel memory (memory used by the OS). This is accomplished by mapping the offsets of the memory used by a process to real memory. This mapping is maintained by mapping tables. The whole process is managed behind the scenes by the OS. For example, a process may request the value at offset 5000, but the OS translates this to offset 134000 in real memory and retrieves the data from there. The results is that each process has its own addressing space which it can use as it likes.

  • Memory Mapping

Memory mapping is used to map image and data files into a processes address space. In memory mapping, the contents of a file are linked directly into the virtual address space of a process. Memory-mapped files: reading files as if they were fully loaded in memory and the OS performing reads behind the scene

  • Fair Physical Memory Allocation

The memory management subsystem allows each running process in the system a fair share of the physical memory of the system,

The ability for multiple processes to have access to the same memory. Modifications to this shared memory are immediately visible for all processes.

7 - Documentation / Reference

data_storage/virtual_memory.txt · Last modified: 2018/06/08 14:51 by gerardnico