Computer Memory Management in Operating Systems
Computer Memory Management in Operating Systems
• It is a virtual address.
• It is the actual location in the memory.
• It is visible to the user.
• It is not visible to the user.
• It is generated by the CPU.
• It is computed by the MMU.
• It is used by the user to access the physical address inside the
memory.
• It is not accessible directly by the user.
• The set of all logical addresses generated by the CPU is called the
logical address space.
• The set of all physical addresses corresponding to the logical addresses
in the logical address space is called the physical address space.
Swapping
• Swapping in OS is done to get access to data present in
secondary memory and transfer it to the main memory so
that it can be used by the application programs.
• A process must be loaded into memory in order to execute.
• If there is not enough memory available to keep all running
processes in memory at the same time, then some processes
that are not currently using the CPU may have their memory
swapped out to a fast local disk called the backing store.
• Swapping is the process of moving a process from memory
to backing store and moving another process from backing
store to memory. Swapping is a very slow process compared
to other operations .
Swapping of two processes using a disk as a backing store
Swap In:
• The method of removing a process from secondary
memory (Hard Drive) and restoring it to the main
memory (RAM ) for execution is known as the Swap
In method.
Swap Out:
• It is a method of bringing out a process from the main
memory(RAM) and sending it to the secondary
memory(hard drive) so that the processes with higher
priority or more memory consumption will be
executed known as the Swap Out method.
• Note:- Swap In and Swap Out method is done
by Medium Term Scheduler(MTS).
Advantages of Swapping in OS:
• Swapping in OS helps in achieving the goal of Maximum
CPU Utilization.
• Swapping ensures proper memory availability for every
process that needs to be executed.
• Swapping helps avoid the problem of process starvation
means a process should not take much time for execution
so that the next process should be executed.
• CPU can perform various tasks simultaneously with the help
of swapping so that processes do not have to wait much
longer before execution.
• Swapping ensures proper RAM(main memory) utilization.
• Swapping creates a dedicated disk partition in the hard
drive for swapped processes which is called swap space.
Contiguous Memory Allocation
• The user program deals with logical addresses (0 to max); it never sees
the real physical addresses (R to R+max)
– Say the logical address 25
– Execution-time binding occurs when reference is made to location in memory
– Logical address bound to physical addresses
Dynamic relocation using a
relocation register
14000
Relocatable
code
Contiguous Allocation
Multiple processes resides in memory
Contiguous Allocation
• Multiple-partition allocation
– Divide memory into several Fixed size partition
– Each partition stores one process
– Degree of multiprogramming limited by number of
partitions
– If a partition is free, load process from job queue
– MFT (IBM OS/360)
Contiguous Allocation (Cont.)
• Multiple-partition allocation
– Variable partition scheme
– Hole – block of available memory; holes of various size are scattered
throughout memory
– Keeps a table of free memory
– When a process arrives, it is allocated memory from a hole large
enough to accommodate it
– Process exiting frees its partition, adjacent free partitions combined
– Operating system maintains information about:
a) allocated partitions b) free partitions (hole)
OS OS OS OS OS
• Best-fit: Allocate the smallest hole that is big enough; must search entire list,
unless ordered by size
– Produces the smallest leftover hole
• Worst-fit: Allocate the largest hole; must also search entire list
– Produces the largest leftover hole
Hardware Support for Relocation
and Limit Registers
• Relocation registers used to protect user processes from each other, and from changing
operating-system code and data
• Relocation register contains value of smallest physical address
• Limit register contains range of logical addresses – each logical address must be less
than the limit register
• Context switch
• MMU maps logical address dynamically
Fragmentation
What is Fragmentation?
When processes are moved to and from the main memory, the
available free space in primary memory is broken into smaller pieces.
This happens when memory cannot be allocated to processes
because the size of available memory is less than the amount of
memory that the process requires. Such blocks of memory stay
unused. This issue is called fragmentation.
Fragmentation is of the following two types:
1. External Fragmentation:
The total amount of free available primary is sufficient to reside a
process, but can not be used because it is non-contiguous. External
fragmentation can be decreased by compaction or shuffling of data in
memory to arrange all free memory blocks together and thus form
one larger free memory block.
2. Internal Fragmentation:
Internal fragmentation occurs when the
memory block assigned to the process is larger
than the amount of memory required by the
process. In such a situation a part of memory is
left unutilized because it will not be used by any
other process. Internal fragmentation can be
decreased by assigning the smallest partition of
free memory that is large enough for allocating to
a process.
What is Paging?
• A computer system can address and utilize more memory than the size
of the memory present in the computer hardware. This extra memory
is referred to as virtual memory. Virtual memory is a part of secondary
memory that the computer system uses as primary memory. Paging
has a important role in the implementation of virtual memory.
• The process address space is a set of logical addresses that every
process refers to in its code. Paging is a technique of memory
management in which the process address space is broken into blocks.
All the blocks are of the same size and are referred to as “pages”. The
size of a page is a power of 2 and its value is in the range of 512
bytes to 8192 bytes. The size of a process is measured in terms of the
number of pages.
• A similar division of the main memory is done into blocks of fixed size.
These blocks are known as “frames” and the size of a frame is the
same as that of a page to achieve optimum usage of the primary
memory and for avoiding external fragmentation.
Advantages of Paging:
• Paging decreases external fragmentation.
• Paging is easy to implement.
• Paging adds to memory efficiency.
• Since the size of the frames is the same as that of
pages, - swapping becomes quite simple.
• Paging is useful for fast accessing data.
Disadvantages of Paging:
• May cause Internal fragmentation
• Page tables consume additional memory.
• Multi-level paging may lead to memory reference
overhead.
Memory Protection
• Memory protection implemented by associating
protection bit with each frame to indicate if read-
only or read-write access is allowed
– Can also add more bits to indicate page execute-only,
and so on
• Valid-invalid bit attached to each entry in the page
table:
– “valid” indicates that the associated page is in the
process logical address space, and is thus a legal page
– “invalid” indicates that the page is not in the process
logical address space
– Or use page-table length register (PTLR)
• Any violations result in a trap to the kernel
• Insertion of an additional bit – a Valid/Invalid
bit, can help protect a paging process.
Whereas, we can achieve Paging Memory
protection by associating protection bits with
each page.
Valid (V) or Invalid (I) Bit in a Page Table
Segmentation
Advantages
• Excellent efficiency and Less complexity
• Easy to use and understand
• Simple data structures can be used to implement
• Used as the benchmark for other algorithms
Disadvantages
• More time consuming and difficult for error handling
• Need future awareness of the programs, which is not possible every
time
Example:
Perform Optimal Page replacement algorithm using
page reference string 3, 1, 2, 1, 6, 5, 1, 3 with 3-page frames . Find the number of page
faults?
Initially, since all the slots are empty, pages 3, 1, 2 cause a page
fault and take the empty slots.
Page faults = 3
When page 1 comes, it is in the memory and no page fault occurs.
Page faults = 3
When page 6 comes, it is not in the memory, so a page fault occurs
and 2 is removed as it is not going to be used again.
Page faults = 4
When page 5 comes, it is also not in the memory and causes a page
fault. Similar to above 6 is removed as it is not going to be used
again.
page faults = 5
When page 1 and page 3 come, they are in the memory so no page
fault occurs.
Total page faults = 5
Least Recently Used (LRU) Page Replacement Algorithm
• The least recently used page replacement algorithm keeps the track of
usage of pages over a period of time. This algorithm works on the basis of
the principle of locality of a reference which states that a program has a
tendency to access the same set of memory locations repetitively over a
short period of time. So pages that have been used heavily in the past are
most likely to be used heavily in the future also.
• In this algorithm, when a page fault occurs, then the page that has not
been used for the longest duration of time is replaced by the newly
requested page.
Advantages
• It is open for full analysis
• Doesn’t suffer from Belady’s anomaly
• Often more efficient than other algorithms
Disadvantages
• It requires additional data structures to be implemented and More
complex
Example: Let’s see the performance of the LRU on the
reference string of 3, 1, 2, 1, 6, 5, 1, 3 with 3-page frames:
Initially, since all the slots are empty, pages 3, 1, 2 cause a page fault
and take the empty slots.
Page faults = 3
When page 1 comes, it is in the memory and no page fault occurs.
Page faults = 3
When page 6 comes, it is not in the memory, so a page fault occurs and
the least recently used page 3 is removed.
Page faults = 4
When page 5 comes, it again causes a page fault and page 1 is
removed as it is now the least recently used page.
Page faults = 5
When page 1 comes again, it is not in the memory and hence page 2 is
removed according to the LRU.
Page faults = 6
When page 3 comes, the page fault occurs again and this time page 6 is
removed as the least recently used one.
Total page faults = 7
Thrashing
• Thrashing is the state of a process where there is high paging
activity. A process that is spending more time paging than
executing is said to be thrashing.
• This results in low CPU utilization, and the operating system
responds by attempting to increase the degree of
multiprogramming.
Page Replacement Algorithms and Effect of Thrashing
• To deal with page faults, the operating system uses either the
global frames replacement algorithm or the local frames
replacement algorithm to bring in enough pages in the main
memory. Let’s see how these replacement algorithms affect
thrashing.
1. Global Page Replacement
• The Global Page replacement has the ability to bring any
page, and once Thrashing in the operating system is detected,
it attempts to bring more pages. As a result, no process can
acquire enough frames, and the thrashing in the operating
system. To summarise, when Thrashing occurs in the
operating system, the global page replacement technique is
ineffective.
2. Local Page Replacement
• In contrast to the Global Page Replacement, the Local
Page Replacement will select pages that are part of that
process. As a result, there is a chance that the operating
system’s thrashing will be reduced. As previously
demonstrated, there are several disadvantages to using
Local Page replacement. As a result, local page
replacement is simply an alternative to global page
replacement.
Causing of Thrashing in OS
p d
m-n n
Logical address 4
User’s view (1*4+0)
Physical address:
(6*4+0)=24
Run time address binding
Logical address 13
(3*4+1)
Physical address:
(2*4+1)
n=2 and m=4 32-byte
memory and 4-byte pages
Paging
• External fragmentation??
• Calculating internal fragmentation
– Page size = 2,048 bytes
– Process size = 72,766 bytes
– 35 pages + 1,086 bytes
– Internal fragmentation of 2,048 - 1,086 = 962 bytes
• So small frame sizes desirable?
– But increases the page table size
– Poor disk I/O
– Page sizes growing over time
• Solaris supports two page sizes – 8 KB and 4 MB
• User’s view and physical memory now very different
– user view=> process contains in single contiguous memory space
• By implementation process can only access its own memory
– protection
• Each page table entry 4 bytes (32 bits) long
• Each entry can point to 232 page frames
• If each frame is 4 KB
• The system can address 244 bytes (16TB) of
physical memory
User’s view
System’s view
RAM RAM
Before allocation After allocation
Implementation of Page Table
• For each process, Page table is kept in main memory
• Page-table base register (PTBR) points to the page table
• Page-table length register (PTLR) indicates size of the page
table
• In this scheme every data/instruction access requires two
memory accesses
– One for the page table and one for the data / instruction
• The two memory access problem can be solved by the use
of a special fast-lookup hardware cache called associative
memory or translation look-aside buffers (TLBs)
Associative memory
Associative Memory
• Associative memory – parallel search
Page # Frame #
• On a TLB miss, value is loaded into the TLB for faster access next time
– Replacement policies must be considered (LRU)
– Some entries can be wired down for permanent fast access
• Some TLBs store address-space identifiers (ASIDs) in each TLB entry – uniquely identifies each
process (PID) to provide address-space protection for that process
– Otherwise need to flush at every context switch
Paging Hardware With TLB
Effective Access Time
• Associative Lookup = time unit
– Can be < 10% of memory access time
• Hit ratio =
– Hit ratio – percentage of times that a page number is found in the associative
registers; ratio related to size of TLB
• Consider = 80%, = 20ns for TLB search, 100ns for memory access
• Consider = 80%, = 20ns for TLB search, 100ns for memory access
– EAT = 0.80 x 120 + 0.20 x 220 = 140ns
• Consider better hit ratio -> = 98%, = 20ns for TLB search, 100ns for
memory access
– EAT = 0.98 x 120 + 0.02 x 220 = 122ns
Memory Protection
• Memory protection implemented by associating protection
bit with each frame to indicate if read-only or read-write
access is allowed
– Can also add more bits to indicate page execute-only, and so on
ptr
Shared
memory
Structure of the Page Table
• Memory requirement for page table can get huge using straight-forward
methods
– Consider a 32-bit logical address space as on modern computers
– Page size of 4 KB (212)
– Page table would have 1 million entries 220 (232 / 212)
– If each entry is 4 bytes -> 4 MB of physical address space / memory for page table
alone
• That amount of memory used to cost a lot
• Don’t want to allocate that contiguously in main memory
• Hierarchical Paging
d
p1
p2
Pentium II
Address-Translation Scheme
Pentium II
64-bit Logical Address Space
• Even two-level paging scheme not sufficient
• If page size is 4 KB (212)
– Then page table has 252 entries
– If two level scheme, inner page tables could be 210 4-byte entries
– Address would look likepage
inner
outer page page offset
p1 p2 d
42 10 12
SPARC (32 bits), Motorola 68030 support three and four level paging respectively
Hashed Page Tables
• Each element contains (1) the page number (2) the value of the
mapped page frame (3) a pointer to the next element
Address space ID
Segmentation
• Memory-management scheme that supports user view of memory
• A program is a collection of segments
– A segment is a logical unit such as:
main program Compiler generates the
procedure segments
Loader assign the seg
function
method
object
local variables, global variables
common block
stack
symbol table
arrays
User’s View of a Program
User specifies each address
by two quantities
(a) Segment name
(b) Segment offset
4
1
3 2
4
Logical
address
space user space physical memory space
• Long term scheduler finds and allocates memory for all segments of a program
• Variable size partition scheme
Memory image
Executable file and virtual address
Symbol table
Name address
SQR 0
a.out
SUM 4 Virtual address
space
Paging view
0 Load 0
4 ADD 4
Segmentation view
<CODE, 0> Load <ST,0>
<CODE, 2> ADD <ST,4>
Segmentation Architecture
• Logical address consists of a two tuple:
<segment-number, offset>
• Segment table – maps two-dimensional logical address to
physical address;
• Each table entry has:
– base – contains the starting physical address where the segments
reside in memory
– limit – specifies the length of the segment
• Segment-table base register (STBR) points to the segment
table’s location in memory
• Segment-table length register (STLR) indicates number of
segments used by a program;
segment number s is legal if s < STLR
Example of Segmentation
Segmentation Hardware
Example of Segmentation
Segmentation Architecture
• Protection
• Protection bits associated with segments
– With each entry in segment table associate:
• validation bit = 0 illegal segment
• read/write/execute privileges
• Code sharing occurs at segment level
• Since segments vary in length, memory allocation is a
dynamic storage-allocation problem
– Long term scheduler
– First fit, best fit etc
• Fragmentation
Segmentation with Paging
Key idea:
Segments are splitted into multiple pages
Page table=220
entries
Example: The Intel Pentium
Large virtual
space
Small memory
Classical paging
• Process P1 arrives
• Requires n pages => n frames must be
available
• Allocate n frames to the process P1
• Create page table for P1
• Pager
Page Table When Some Pages
Are Not in Main Memory
….
ii Disk
address
• During address translation, if valid–invalid bit in page table entry
is i page fault
Page Fault
• If the page in not in memory, first reference to that page will trap to
operating system:
page fault
• Page fault
– No free frame
– Terminate? swap out? replace the page?
• Page replacement – find some page in memory, not really in use, page it out
– Performance – want an algorithm which will result in minimum number of page faults
Allocation of frames
• Depends on multiprogramming level
P2
Need For Page Replacement
P1
P2
PC
Basic Page Replacement
3. Bring the desired page into the (newly) free frame; update the page and
frame tables
4. Continue the process by restarting the instruction that caused the trap
Note now potentially 2 page transfers for page fault – increasing Effective
memory access time
Page Replacement
5
5 6
6
Page Replacement
5 5
6
6
Evaluation
• Evaluate algorithm by running it on a particular string of
memory references (reference string) and computing the
number of page faults on that string
– String is just page numbers, not full addresses
– Repeated access to the same page does not cause a page fault
• Trace the memory reference of a process
0100, 0432, 0101, 0612, 0102, 0104, 0101, 0611, 0102
• Page size 100B
• Reference string 1, 4, 1, 6, 1, 6
Associates a time with each frame when the page was brought
into memory
Limitation:
A variable is initialized early and constantly used
FIFO Page Replacement
3 frames (3 pages can be in memory at a time)
15 page faults
9 page faults
Least Recently Used (LRU) Algorithm
0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
LRU Approximation Algorithms
• LRU needs special hardware and still slow
• Reference bit
– With each page associate a bit, initially = 0
– When page is referenced bit set to 1
• Additional reference bit algorithm
• Second-chance algorithm
– Generally FIFO, plus hardware-provided reference bit
– Clock replacement
– If page to be replaced has
• Reference bit = 0 -> replace it
• reference bit = 1 then:
– set reference bit 0, leave page in memory (reset the time)
– replace next page, subject to same rules
Second-Chance (clock) Page-Replacement Algorithm
FIFO Example –
Program - 5 pages,
3 frames of Memory allocated
1 2 3 4 1 2 5 1 2 3 4 5
9 1 1 1 4 4 4 5 5 5 5 5 5
faults 2 2 2 1 1 1 1 1 3 3 3
3 3 3 2 2 2 2 2 4 4
FIFO Example –
Program - 5 pages,
4 frames of Memory allocated
1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 4 4 4 5 5 5 5 5 5
2 2 2 1 1 1 1 1 3 3 3
3 3 3 2 2 2 2 2 4 4
10 1 1 1 1 1 1 5 5 5 5 4 4
faults 2 2 2 2 2 2 1 1 1 1 5
3 3 3 3 3 3 2 2 2 2
4 4 4 4 4 4 3 3 3
Belady's Anomaly
# of Page Faults
Number of Frames
cs431-cotter 166
Belady’s Anomaly
• This most unexpected result is known as
Belady’s anomaly – for some page-
replacement algorithms, the page fault rate
may increase as the number of allocated
frames increases
3 6 4 3 9
5 3 6 4 3
In memory
4 frames 6 5 3 6 4
5 5 6
5
Global vs. Local Allocation
• Frames are allocated to various processes
• Local replacement – each process selects from only its own set of allocated
frames
– More consistent per-process performance
– But possibly underutilized memory
• Global replacement – process selects a replacement frame from the set of all
frames; one process can take a frame from another
– But then process execution time can vary greatly
– But greater throughput ----- so more common
• Processes can not control its own page fault rate
– Depends on the paging behavior of other processes
Thrashing
• If a process uses a set of “active pages”
– Number of allocated frames is less than that
• Page-fault
– Replace some “active” page
– But quickly need replaced “active” frame back
– Quickly a page fault, again and again
– Thrashing a process is busy swapping pages in and out
• if D > m Thrashing
• Policy if D > m, then suspend or swap out one of the processes
Page-Fault Frequency
• More direct approach than WSS
• Establish “acceptable” page-fault frequency (PFF) rate
and use local replacement policy
– If actual rate too low, process loses frame
– If actual rate too high, process gains frame