1. Abir Naskar | 10MA60R21 | March 26, 2014
Virtual Memory
COMPUTER SCIENCE AND DATA PROCESSING
2. PAGE 1
Abstract
Virtual memory is the concept by which we can use secondary storage as an extension of primary
storage, by using paging concept. This memory management technique is implemented by using
hardware and software. In this report I am going to present that how virtual memory helps us to
make computer multitasking, and how to translate the virtual address to physical address and store
them in main memory. How to handle fault and replace page. The basic structure of page table.
And the basic difference between cache memory and virtual memory. And lastly some advantage
and disadvantages of virtual memory.
Introduction
Virtual Memory
Virtual (or logical) memory is a concept that, when implemented by a computer and its operating
system, allows programmers to use a very large range of memory or storage addresses for stored
data. The computing system maps the programmer's virtual addresses to real hardware storage
addresses. Usually, the programmer is freed from having to be concerned about the availability of
data storage.
In addition to managing the mapping of virtual storage addresses to real storage addresses, a
computer implementing virtual memory or storage also manages storage swapping between active
storage (RAM) and hard disk or other high volume storage devices. Data is read in units called
"pages" of sizes ranging from a thousand bytes (actually 1,024 decimal bytes) up to several
megabytes in size. This reduces the amount of physical storage access that is required and speeds
up overall system performance.
Cache Memory
A small amount of high-speed memory residing on or close to the CPU is called Cache Memory.
Cache memory supplies the processor with the most frequently requested data and instructions.
Level 1 cache (primary cache) is the cache closest to the processor. Level 2 cache (secondary cache)
is the cache second closest to the processor and is usually on the motherboard.
Cache memory helps to alleviate the gap between the speed of a CPU's megahertz rating and the
ability of RAM to respond and deliver data. It reduces the frequency that the CPU must wait for
data from the main memory.
3. PAGE 2
Memory Hierarchy
The term memory hierarchy is used in computer architecture when discussing performance issues
in computer architectural design, algorithm predictions, and the lower level programming
constructs such as involving locality of reference. This distinguish each level of hierarchy by access
time or response time. The faster memory costs higher.
Typical memory hierarchy is:
CPU registers (8-256 registers): speed is near about 500 ps.
L1 cache (64KB): speed is less than 1 ns.
L2 cache (256MB): speed is 3-10 ns.
L3 cache (2MB – 4MB): speed is 10-20 ns.
RAM/main memory (4 - 16GB): speed is 50-100 ns.
Disk storage (4 - 16TB): speed is 5-10 ms.
Cache and Virtual memory
Cache Memory Virtual Memory
Is a hardware or processor. Is simulated on hard drive maintained by OS.
Data reads in unit called “block”. Data reads in unit called “pages”.
Stores memory access by CPU. Storage swapping between active storage and
hard disk.
Reduces the frequency that the CPU must wait
for data from the main memory.
Reduces the amount of physical storage access
that is required.
Performance-wise faster. Performance-wise slower.
System with only physical memory
In early days when virtually memory is not used, the CPU directly generates the physical address.
For example it is used in, most of the Cray machine, early PC, nearly all embedded systems etc.
The problems then was to run multi-programming or multi-tasking process or time-sharing tasks.
The program size should be less than the size of the RAM.
4. PAGE 3
Figure 1: system with only physical memory
System with virtual memory
In present days we use virtual memory system is used. Here we have to go through from an
intermediate state which is known as address translation. That is CPU first generates virtual
address and before it mapped to main memory it have to go through a translation process where
hardware convert virtual address to physical address via an OS managed look-up table which is
called page table. Page table has provision to keeping information about main memory address as
well as disk memory address. It use a valid bit to detect where the location is.
In the modern PCs, workstations, servers it is used.
5. PAGE 4
Figure 2: system with virtual memory
Paging
In computer science Paging is a Memory Management technique by which computer can store and
retrieve data from secondary storage for the use of main memory. Paging is an important part of
virtual memory implementation in Operating Systems, allowing them to use the disk storage for
the data that does not fit into the physical RAM. The main functions of Paging are performed when
a program tries to access a page that are currently not in RAM. This situation is known as page
fault. The operating system must take control and handle the page fault, in a manner invisible to
the program. Therefore, the OS must
1. Determine the location of the data in secondary storage.
2. Obtain an empty page frame in RAM to use as a container for the data.
3. Load the requested data into the available page frame.
4. Update page table to show new data.
5. Return control to the program.
6. PAGE 5
Page Table
Page table is data structure used to store mapping between virtual address to physical address. It is
used to translate logical address to physical address. The entries of page table are address which
contains pointer to the location of the page in memory (RAM or disk), valid bit which says that the
page is in RAM or in disk, dirty bit which is set if the page is modified at least once, referenced bit
which is used if page has been referenced with either read or write, this bit used to support
software replacement policies. Another bit is protection bit or access right bit which is used to
restrict the access, for example, read only access, write only access, system only access etc.
Figure 3: page table
Address Mapping
Virtual address has two parts virtual page number and page offset. The number of pages
addressable to the virtual address is virtually unlimited where as the number of pages addressable
by the physical address is limited. CPU generates virtual address.
A virtual address has two parts, virtual page number and offset. Virtual page number is index into a
page table. Page table entry contains page frame number. Physical address will be then
concatenation of page frame number and offset.
7. PAGE 6
Figure 4: address mapping
In the above image p defines virtual page number, o is offset of the page, f is the page frame
number.
Page translation example:
assume a 32bit address space, and page size is 4KB (log2(4096) = 12bits)
for a process to address the full logical address space, need 20bit virtual page number and 12 bits
offset.
Let we want to transform the virtual address 0x12345678. And let the offset contains 0x678. If the
page table entry of 12345 contains 0x01000 then the physical address will be 0x01000678.
Page Fault
Page fault occurs when page is on disk rather than memory.
When page fault occurs then the OS is invoked to move page from disk to main memory.
At the same time current process is suspended and other can resume. And OS has full control over
placement.
When a process is created, the OS creates space for all the pages of a process in the disk. This disk
space is called swap space. It also creates a data structure to record where each virtual page is
stored in the disk. The data structure may be part of the page table or separate. The OS also creates
a data structure that tracks which processes and which virtual addresses use each physical page. If
all pages in main memory are in use, the OS must choose a page to replace based on the past
history. The replaced pages are written to the swap space of the disk.
8. PAGE 7
Figure 5: before page fault Figure 6: after page fault
Page Replacement
When a fault occurs, the OS loads the faulted page from disk into a page of memory.
At some point, the process has used all of the page frames it is allowed to use.
When this happens, the OS must replace page for each page faulted in. That is, it must select a
page to throw out of primary memory to make room.
There are several page replacement algorithms, like FIFO, LRU (least recent used), Least
Frequently Used, optimal or minimum used etc.
the goal of the page replacement is to reduce the fault rate by selecting the best victim page to
remove.
Example
Let us use here LRU (least recently used technique).
Say we have the page reference string is, 1 2 3 4 2 1 5 6 2 1 2 3 7 6
assuming there is 4 page frame available. Then the procedure will be as follows.
Frame 1 1* 1* 1* 1* 1* 1 1 1 1* 1 1 1 1* 6
Frame 2 2 2 2 2 2 2 2* 2 2 2 2 2 2*
Frame 3 3 3 3 3* 5 5 5 5* 5* 3 3 3
Frame 4 4 4 4 4* 6 6 6 6 6* 7 7
faults Y Y Y Y N N Y Y N N N Y Y Y
9. PAGE 8
Advantage & Disadvantage
We can run more applications at once.
We can run larger applications with less real RAM.
We have large memory in disk that is larger virtual memory.
More efficient use of memory in this technique.
There is no limit or degree of multiprogramming.
- Applications run slower.
- It takes more time to switch between applications.
- Less hard drive space for your use.
References
• Stallings W. (2006): “Operating Systems: Internals and Design Principles (7
th
Edition)”.
Prentice Hall.
• Tanenbaum A. S. (2013): “Modern Operating System (3
rd
edition)”, Pearson Education,
pp.175-247.
• Cragon, Harvey G. (1996). Memory Systems and Pipelined Processors. Jones and Bartlett
Publishers. ISBN 0-86720-474-5.
• Hannessy J. L, Patterson D. A. (2012). Computer Architecture a quantitative approach
.Elsevier. ISBN 978-0-12-383872-8.
• URL: http://windows.microsoft.com/en-us/windows/what-is-virtual-
memory#1TC=windows-7
• URL: http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Memory/virtual.htm