Virtual Memory - Virtual memory

> Operating System - Kernel (Windows, Unix, Linux) > OS - Virtual Memory

1 - About

A process reserve memory for its use by requesting an allocation from the operating system. The entire reserved memory footprint of a program is referred to as its Virtual Memory. See history

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.

Advertising

3 - 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.

4 - Virtual Memory Concept

4.1 - Address Space

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

  • the application
  • and its data.

See Process - address space

Advertising

4.2 - 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.

4.3 - 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.

4.4 - Page table (virtual to physical addresses)

5 - 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.

Advertising

6 - Management

6.1 - First offset

Memory begins at 0 (represented in WDM as 0000000000000000).

6.2 - Memory mapping

You can view memory mappings by opening the device manager, clicking view, and then resources by type.

  • launch “devmgmt.msc”,
  • select Resources by Connection in the View Menu,
  • Expand the Memory node.

On my laptop, the primary consumer of mapped device memory is, unsurprisingly, the video card, which consumes 255MB.

Range Calculation:

  • Headecimal: E0000000-EFFFFFFF
  • ‭‭To decimal: 3758096384‬-‭4026531839‬
  • ‭Diff: 268435455‬ byte
  • ‭In Mb: 268435455/1024/1024 = 255 Mb

7 - Documentation / Reference

os/memory/virtual.txt · Last modified: 2019/01/05 16:11 by gerardnico