Professional Documents
Culture Documents
AdriasJ MIT VirtualMemory Segmentation Paging Page Replacement
AdriasJ MIT VirtualMemory Segmentation Paging Page Replacement
AdriasJ MIT VirtualMemory Segmentation Paging Page Replacement
With the help of the memory management technology known as virtual memory,
secondary memory can be used as like it were a component of the main memory. A
frequent method in an operating system for a computer is virtual memory (OS).
Figure 1:
Virtual
memory
can act in
concert
with a
Virtual memory was developed at a time when physical memory, also known
as RAM, was expensive. Since the computer has a limited amount of RAM, running
2 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
multiple programs at the same time will eventually run out of memory. Systems that
use virtual memory use sections of the hard drive to emulate RAM. Virtual memory
allows your system to run larger programs or multiple programs simultaneously, each
temporary as if they had more memory without having to buy more RAM.
Here are some situations where the entire program does not need to be fully
loaded into main memory.
User-written error handlers are used only when there is an error in data or
calculations.
Program may be used rarely at a certain options and features.
Many tables are assigned a fixed address space even if only a small portion of
the table is actually used.
Being able to run a program that is only partially in memory negates many
benefits.
Less I/O is required to load or swap each user program into memory.
Programs are no longer limited by the amount of physical memory available.
Each user program used less physical memory, allowing more programs to run
simultaneously, with a corresponding increase
How virtual memory works
Virtual memory works using both hardware and software. When an application
is used, data from that program is stored at physical addresses using RAM. The
memory management unit (MMU) maps addresses to RAM and translates addresses
automatically. For example, an MMU can map a logical address space to a
corresponding physical address.
Figure 2:
MMU can
map a logical
address
space to a
Source: https://www.baeldung.com/wp-content/uploads/sites/4/2021/05/Capture.png
If at some point you need RAM space for something more pressing, it can swap
data out of RAM into virtual memory. A computer's memory manager is responsible
3 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
for keeping track of movements between physical and virtual memory. When that data
is needed again, MMU on the computer uses a context switch to continue execution.
The main advantage of virtual memory is that the operating system can load
programs larger than its physical memory. This gives the user the impression that the
computer has unlimited memory. It also provides memory protection.
Implementing mapping operations requires the use of page tables and
translations in virtual memory. A page table is a data structure that stores page tables
called page table entries (PTEs). The purpose of page tables is to map virtual
addresses to physical addresses. This is a contiguous block, the smallest unit of virtual
memory.
To create a memory page, divide the memory page into frames of equal size.
Each frame references a respective physical address frame number, frame offset, or
absolute address.
Translation is the process of translating virtual addresses into physical
addresses using page tables:
Figure 3:
Translation
Process
Source: https://www.baeldung.com/wp-content/uploads/sites/4/2021/05/pava.png
When copying virtual memory to physical memory, the operating system shares
memory with a fixed number of paging or paging file addresses. Each page is stored
on disk, and when the page is needed, the operating system copies it from disk to
main memory, translating the virtual address to a real address.
However, the process of swapping virtual memory to physical memory is very
slow. This usually means that using virtual memory will degrade performance
4 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
significantly. Because of swapping, computers with more RAM are believed to perform
better. To speed up the process of reading data, virtual memory can use cache
memory. Use virtual cache because physical cache does not guarantee fast
processing. The CPU is directly connected to the cache and looks up virtual addresses
in the cache avoiding translations.
Translation is only done if the address is not found. Finally, each program has
its own virtual cache:
Figure 4:
Translation
Process
Source: https://www.baeldung.com/wp-content/uploads/sites/4/2021/05/cache-1.png
Figure 5 Memory
Management Unit
translate Virtual
Address into
Physical
Addresses.
Source: https://www.techtarget.com/searchstorage/definition/virtual-memory.
Figure 6. This image shows an example of how physical memory is separated by a virtual memory
manager.
Source: https://www.techtarget.com/searchstorage/definition/virtual-memory.
Figure 7.This chart shows how virtual RAM (virtual memory) compares to RAM
(physical memory).
Users can actively add RAM to a computer by buying and installing more RAM
chips. This is useful if they are experiencing slowdowns due to memory swaps
happening too often. The amount of RAM depends on what is installed on a computer.
Virtual memory, on the other hand, is limited by the size of the computer's hard drive.
Virtual memory settings can often be controlled through the OS.
In addition, RAM uses swapping techniques, while virtual memory uses paging.
While physical memory is limited to the size of the RAM chip, virtual memory is limited
by the size of the hard disk. RAM also has direct access to the CPU, while virtual RAM
does not.
Source: https://www.gatevidyalay.com/wp-content/uploads/2018/11/Non-Contiguous-Memory-
Allocation-Techniques.png
Paging divides memory into sections or paging files. When a computer uses
up its available RAM, pages not in use are transferred to the hard drive using a swap
file. A swap file is a space set aside on the hard drive to be used as the virtual memory
extension for the computer's RAM. When the swap file is needed, it is sent back to
RAM using a process called page swapping. This system ensures the computer's OS
and applications do not run out of real memory. The maximum size of the page file
can be 1 ½ to four times the physical memory of the computer.
The virtual memory paging process uses page tables, which translate the virtual
addresses that the OS and applications use into the physical addresses that the MMU
uses. Entries in the page table indicate whether the page is in RAM. If the OS or a
program does not find what it needs in RAM, then the MMU responds to the missing
9 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
memory reference with a page fault exception to get the OS to move the page back to
memory when it is needed. Once the page is in RAM, its virtual address appears in
the page table.
Segmentation is also used to manage virtual memory. This approach divides
virtual memory into segments of different lengths. Segments not in use in memory can
be moved to virtual memory space on the hard drive. Segmented information or
processes are tracked in a segment table, which shows if a segment is present in
memory, whether it has been modified and what its physical address is. In addition,
file systems in segmentation are only made up of segments that are mapped into a
process's potential address space.
Segmentation and paging differ as a memory model in terms of how memory is
divided; however, the processes can also be combined. In this case, memory gets
divided into frames or pages. The segments take up multiple pages, and the virtual
address includes both the segment number and the page number.
Other page replacement methods include first-in-first-out (FIFO), optimal
algorithm and least recently used (LRU) page replacement. The FIFO method has
memory select the replacement for a page that has been in the virtual address for the
longest time. The optimal algorithm method selects page replacements based on
which page is unlikely to be replaced after the longest amount of time; although difficult
to implement, this leads to less page faults. The LRU page replacement method
replaces the page that has not been used for the longest time in the main memory.
How to Increase Virtual Memory in a System
Most operating systems allow users to increase virtual memory from a
configuration page.
In Windows, users can also allow the system to manage the amount of virtual
memory provided dynamically.
Please note that on Windows 11, you can take a less circuitous route to your
Advanced System Settings.
1. Press Windows key + I to
open the Settings app.
2. Head to System > About.
3. Select Advanced system
settings.
4. Under Performance,
select Settings. Open
the Advanced tab.
Under Virtual memory,
select Change. Here are your
Virtual Memory options.
Source: https://www.makeuseof.com/tag/virtual-memory-low-heres-fix/
Source: https://www.makeuseof.com/tag/virtual-memory-low-heres-fix/
("There is a formula for calculating the correct pagefile size. The Initial size is one and
a half (1.5) x the amount of total system memory. The Maximum size is three (3) x the
initial size. So let's say you have 4 GB (1 GB = 1,024 MB x 4 = 4,096 MB) of memory.
The initial size would be 1.5 x 4,096 = 6,144 MB and the maximum size would be 3 x
6,144 = 18,432 MB.".)
Before virtual memory was developed, computers had RAM and secondary
memory. Early computers used magnetic core memory for main memory and magnetic
drums for their secondary memory. Computer memory was expensive and, usually, in
short supply back in the 1940s and 1950s. As computer programs grew in size and
complexity, developers had to worry that their programs would use up all of a
computer's main memory and run out of memory.
In those early days, programmers used a process called overlaying to run
programs that were larger than available memory. Parts of a program that were not
continually in use were set up as overlays that, when needed, would overwrite the
existing overlay in memory. It required extensive programming to make overlaying
work, and that was a key impetus for the development of automated virtual memory.
German physicist Fritz-Rudolf Güntsch has been credited with developing the
concept of virtual memory in 1956 -- though this point has been contested. Güntsch
did, however, end up describing a form of cache memory.
The first apparent real instance of a virtual memory system came from the
University of Manchester in Manchester, England, in its attempt to develop a one-level
storage system for the Atlas computer. The system used paging to map virtual
addresses to a programmer onto the primary memory. Atlas was developed in 1959
and later commissioned in 1962.
In 1961, the first commercial computer with virtual memory was released by the
Burroughs Corp. This version of virtual memory used segmentation, as opposed to
paging.
In 1969, IBM researchers demonstrated that virtual memory overlay systems
worked better than the earlier manual systems. Up until this point, there was still a
debate over this. Mainframes and minicomputers in the 1970s generally used virtual
memory. Virtual memory technology was not included in early PCs because
developers thought running out of memory would not be a problem in those machines.
That assumption proved incorrect. Intel introduced virtual memory in the protected
mode of the 80286 processor in 1982, and paging support when the 80386 came out
in 1985.
What is Segmentation?
Segmentation is a memory management technique that splits up the virtual
address space of an application into chunks.
By splitting the memory up into manageable chunks, the operating system can
track which parts of the memory are in use and which parts are free. This makes
allocating and de-allocating memory much faster and simpler for the operating system.
The segments are of unequal size and are not placed in a contiguous way. As it’s a
non-contiguous memory allocation technique, internal fragmentation doesn’t occur.
The length is decided on the base of the purpose of the segment in a user program.
The logical address in paging is Here, the logical address is divided into
4. divided into a page offset and page section number and limit(size of the
number. segment).
This concept is the same as the paging concept. But unlike paging in
Segmentation, the process is not divided into pages but into large chunks
called segments or modules. The CPU generates this logical address. This logical
address is to access secondary memory. But CPU has to access the main memory. So
what to do now? In this case, address translation is required to convert a logical
address into a physical one.
Segmentation gives user’s view of the process which paging does not give.
Here the user’s view is mapped to physical memory.
There are types of segmentation:
A physical address is an address for finding data in the main memory. So now
the CPU will take the help of the segment table. A segment table is a data structure
for storing the information of all process segments. CPU use a segment table to map
the logical address to a physical address.
In the segment table, there are two types of information
Limit: Specify the length of Segment. (Actual size of a segment)
Then if the value of offset(d)<=Limit. Then only the CPU can read that segment;
else, the error will be there. Offset (d) depicts the size of that segment CPU wants to
read.
Figure Segmentation in the x86 operating system which means accessing the
memory through segments.
Source: https://miro.medium.com/max/876/1*t8a-wUISu7k7Rt9kOEgoZQ.png
The paging technique divides the physical memory (main memory) into fixed-size
blocks that are known as Frames and also divide the logical memory(secondary
memory) into blocks of the same size that are known as Pages.
This technique keeps the track of all the free frames.
The Frame has the same size as that of a Page. A frame is basically a place where
a (logical) page can be (physically) placed.
Source: https://s3.ap-south-1.amazonaws.com/s3.studytonight.com/tutorials/uploads/pictures/
1608530491-71449.png
Each process is mainly divided into parts where the size of each part is the same as
the page size.
There is a possibility that the size of the last part may be less than the page size.
Pages of a process are brought into the main memory only when there is a
requirement otherwise they reside in the secondary storage.
One page of a process is mainly stored in one of the frames of the memory.
Also, the pages can be stored at different locations of the memory but always
the main priority is to find contiguous frames.
The Page table mainly contains the base address of each page in the Physical
memory. The base address is then combined with the page offset in order to define
the physical memory address which is then sent to the memory unit.
Thus page table mainly provides the corresponding frame number (base address
of the frame) where that page is stored in the main memory.
As we have told you above that the frame number is combined with the page
offset and forms the required physical address.
So, The physical address consists of two parts:
1. Page offset(d)
2. Frame Number(f)
where,
Frame number is used to indicate the specific frame where the required page
is stored.
21 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Page Offset indicates the specific word that has to be read from that page.
The Page size (like the frame size) is defined with the help of hardware. It is
important to note here that the size of the page is typically the power of 2 that
varies between 512 bytes and 16 MB per page and it mainly depends on the
architecture of the computer.
If the size of logical address space is 2 raised to the power m and page
size is 2 raised to the power n addressing units then the high order m-n bits
of logical address designates the page number and the n low-order bits
designate the page offset.
Physical Address = Frame number + page offset
The logical address is as follows:
where p indicates the index into the page table, and d indicates the displacement within the
page.
Source: https://s3.ap-south-
1.amazonaws.com/s3.studytonight.com/tutorials/uploads/pictures/ 1608534972-71449.png
The above diagram indicates the translation of the Logical address into the
Physical address. The PTBR (Page Table Base Register ) in the above diagram
means page table base register and it basically holds the base address for the page
table of the current process.
The PTBR is mainly a processor register and is managed by the operating
system. Commonly, each process running on a processor needs its own logical
address space.
But there is a problem with this approach and that is with the time required to
access a user memory location. Suppose if we want to find the location i, we must first
find the index into the page table by using the value in the PTBR offset by the page
22 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
number for I. And this task requires memory access. It then provides us the frame
number which is combined with the page offset in order to produce the actual address.
After that, we can then access the desired place in the memory.
With the above scheme, two memory accesses are needed in order to access
a byte( one for the page-table entry and one for byte). Thus memory access is slower
by a factor of 2 and in most cases, this scheme slowed by a factor of 2.
There is the standard solution for the above problem that is to use a special, small,
and fast-lookup hardware cache that is commonly known as Translation of look-aside
buffer(TLB).
TLB is associative and high-speed memory.
Each entry in the TLB mainly consists of two parts: a key(that is the tag) and a
value.
When associative memory is presented with an item, then the item is compared
with all keys simultaneously. In case if the item is found then the corresponding
value is returned.
The search with TLB is fast though the hardware is expensive.
The number of entries in the TLB is small and generally lies in between 64 and
1024.
Source: https://s3.ap-south-
1.amazonaws.com/s3.studytonight.com/tutorials/uploads/pictures/ 1608632310-71449.png
Advantages of Paging
Given below are some advantages of the Paging technique in the operating system:
Paging mainly allows to storage of parts of a single process in a non-
contiguous fashion.
With the help of Paging, the problem of external fragmentation is solved.
Paging is one of the simplest algorithms for memory management.
Disadvantages of Paging
Disadvantages of the Paging technique are as follows:
In Paging, sometimes the page table consumes more memory.
Internal fragmentation is caused by this technique.
There is an increase in time taken to fetch the instruction since now two
memory accesses are required.
If the size of logical address space is 2 raised to the power m and page size is 2
raised to the power n addressing units then the high order m-n bits of logical
address designates the page number and the n low-order bits designate the
page offset.
The logical address is as follows:
where p indicates the index into the page table, and d indicates the displacement
within the page. The Page size is usually defined by the hardware. The size of the
page is typically the power of 2 that varies between 512 bytes and 16 MB per page.
Source: https://s3.ap-south-
1.amazonaws.com/s3.studytonight.com/tutorials/uploads/pictures /1608616966-71449.png
Source: https://s3.ap-south-
1.amazonaws.com/s3.studytonight.com/tutorials/uploads/ pictures/1608619676-
71449.png
The frame table is a data structure that keeps the information of which frames
are allocated or which frames are available and many more things. This table mainly
has one entry for each physical page frame.
26 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
Source: https://s3.ap-south-
1.amazonaws.com/s3.studytonight.com/tutorials/uploads/ pictures/1608622355-
71449.png
The Operating system maintains a copy of the page table for each process in
the same way as it maintains the copy of the instruction counter and register
contents. Also, this copy is used to translate logical addresses to physical addresses
whenever the operating system maps a logical address to a physical address
manually.
This copy is also used by the CPU dispatcher in order to define the hardware
page table whenever a process is to be allocated to the CPU.
The data structure that is used by the virtual memory system in the operating
system of a computer in order to store the mapping between physical and logical
addresses is commonly known as Page Table.
As we had already told you that the logical address that is generated by the CPU
is translated into the physical address with the help of the page table.
Thus page table mainly provides the corresponding frame number (base address of
the frame) where that page is stored in the main memory.
The above diagram shows the paging model of Physical and logical memory.
1. Hierarchical Paging
Another name for Hierarchical Paging is multilevel paging.
There might be a case where the page table is too big to fit in a contiguous
space, so we may have a hierarchy with several levels.
In this type of Paging the logical address space is broke up into Multiple page
tables.
Hierarchical Paging is one of the simplest techniques and for this purpose, a
two-level page table and three-level page table can be used.
Consider a system having 32-bit logical address space and a page size of 1 KB and
it is further divided into:
Page Number consisting of 22 bits.
Page Offset consisting of 10 bits.
As we page the Page table, the page number is further divided into :
Page Number consisting of 12 bits.
Page Offset consisting of 10 bits.
Given below figure shows the address translation scheme of the Inverted Page
Table:
In this, we need to keep the track of process id of each entry, because many
processes may have the same logical addresses.
Also, many entries can map into the same index in the page table after going
through the hash function. Thus chaining is used in order to handle this.
Example-of Paging
Following steps are followed to translate logical address into physical address-
Step-01:
Page Number specifies the specific page of the process from which CPU wants to
read the data.
Page Offset specifies the specific word on the page that CPU wants to read.
Step-02:
Step-03:
The frame number combined with the page offset forms the required physical address.
Diagram-
The following diagram illustrates the above steps of translating logical address into
physical address-
Demand Paging
A demand paging system is quite similar to a paging system with swapping where
processes reside in secondary memory and pages are loaded only on demand, not in
advance. When a context switch occurs, the operating system does not copy any of
34 | Memory and Virtual Management
Prepared by: JOY R. ADRIAS-MIT
Submitted to: DR. FRANCIS F. BALAHADIA
the old program’s pages out to the disk or any of the new program’s pages into the
main memory Instead, it just begins executing the new program after loading the first
page and fetches that program’s pages as they are referenced.
While executing a program, if the program references a page which is not available in
the main memory because it was swapped out a little ago, the processor treats this
invalid memory reference as a page fault and transfers control from the program to
the operating system to demand the page back into the memory.
Advantages
Following are the advantages of Demand Paging −
If you want to know more about Paging, Please watch YouTube link:
https://youtu.be/LKYKp_ZzlvM (Paging Os) https://youtu.be/pQxZxPFunTo
(PAGING IN OPERATING SYSTEM | OS | NON-CONTIGUOUS MEMORY
ALLOCATION)
Initially, all slots are empty, so when 1, 3, 0 came they are allocated to the
empty slots —> 3 Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults. Then 5 comes,
it is not available in memory so it replaces the oldest page slot i.e 1. —>1
Page Fault. 6 comes, it is also not available in memory so it replaces the
oldest page slot i.e 3 —>1 Page Fault. Finally, when 3 come it is not available
so it replaces 0 1 page fault.
Belady’s anomaly proves that it is possible to have more page faults when
increasing the number of page frames while using the First in First Out (FIFO)
page replacement algorithm. For example, if we consider reference strings
3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4, and 3 slots, we get 9 total page faults, but if we
increase slots to 4, we get 10-page faults.
FIFO explaines in this video: https://www.youtube.com/watch?v=ET43MRKRuYM
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —
> 4 Page faults
0 is already there so —> 0 Page fault. when 3 came it will take the place of 7
because it is not used for the longest duration of time in the future.—>1 Page
fault. 0 is already there so —> 0 Page fault. 4 will takes place of 1 —> 1
Page Fault.
Now for the further page reference string —> 0 Page fault because they are
already available in the memory.
Optimal page replacement is perfect, but not possible in practice as the
operating system cannot know future requests. The use of Optimal Page
replacement is to set up a benchmark so that other replacement algorithms
can be analyzed against it.
OPTIMAL Explain in this video:
https://www.youtube.com/watch?v=L8BEoRRUVRE