Chapter 4
Chapter 4
Memory Management
Hawassa University
2
2
Introduction
• The main purpose of a computer system is to execute programs.
These programs, together with the data they access, must be at least
partially in main memory during execution.
• To improve both the utilization of the CPU and the speed of its
response to users, a general-purpose computer must keep several
processes in memory.
• Memory (RAM) is important resource that must be carefully
managed.
• Memory management is the functionality of an operating system
which handles or manages primary memory.
• More RAM is always better even systems that run parallel queries
and populate full-text indexes require more RAM. 3
Cont..
• Physical memory (RAM) is a form of very fast, but volatile data
storage.
• Most computers have memory hierarchy from very fast, volatile,
small amount of storage to slow, non-volatile, large amount of
storage.
4
Cont..
• If only a few processes can be kept in main memory, then much
of the time all processes will be waiting for I/O and the CPU will
be idle.
• Hence, memory needs to be allocated efficiently in order to pack
as many processes into memory as possible.
• Memory Manager
o Responsible to manage memory hierarchy
o Keep track of which memory is in use and which are not in
use.
o Manage swapping between main memory and disk when
RAM is too small to hold all process.
5
Overlays
• The main problem in Fixed partitioning is the size of a process has to
be limited by the maximum size of the partition, which means a
process can never be span over another.
• In order to solve this problem, people have used some solution which
is called as Overlays.
• The concept of overlays is that whenever a process is running it will
not use the complete program at the same time, it will use only some
part of it.
• Sometimes it happens that compare to the size of the biggest
partition, the size of the program will be even more, then, in that
case, you should go with overlays.
• So, overlay is a technique to run a program that is bigger than the
size of the physical memory by keeping only those instructions and
data that are needed at any given time.
6
Cont..
• Divide the program into modules in such a way that not all
modules need to be in the memory at the same time.
Advantage
• Reduce memory requirement
• Reduce time requirement
Disadvantage
• Overlap map must be specified by programmer
• Programmer must know memory requirement
• Overlapped module must be completely disjoint
• Programming design of overlays structure is complex and not possible
in all cases 7
Basic Memory Management
Memory management requirements:
• Relocation: different location for swap out and in.
• Protection: avoid processes from interfering.
• Sharing: Allow several processes to access the same portion
of memory.
• Logical organization: Almost invariably, main memory in a
computer system is organized as a linear, or one-dimensional
address space, consisting of a sequence of bytes or words.
Secondary memory, at its physical level, is similarly
organized.
9
Cont….
A pair of base and limit registers define the logical address space
10
Logical address VS physical address
• During execution of process we can have two address: logical and
physical addresses.
• An address generated by the CPU is a logical address.
• The address actually available on memory unit is a physical
address.
• Logical address is also known a Virtual address.
• The set of all logical addresses generated by a program is referred
to as a logical address space.
• The set of all physical addresses corresponding to these logical
addresses is referred to as a physical address space 11
Cont…
• The run-time mapping from virtual to physical address is done by
the memory management unit (MMU).
• Memory management unit (MMU) is the hardware that maps
virtual to physical address.
• The process size must be less than the physical address space.
14
Cont…
15
Swapping
• Swapping is a mechanism in which a process can be swapped
temporarily out of main memory to a backing store, and vice versa.
• Let us assume that the user process is of size 100KB and the
backing store is a standard hard disk with transfer rate of 1 MB per
second.
19
Memory management technique
• How to allocate the main memory to various process?
• The main memory must accommodate both the operating system
and the various user processes. Therefore, we need to allocate main
memory in the most efficient way.
• In this figure we have a set of process wait in input queue that is
reside in secondary memory and waiting to load in the main
memory.
20
Cont…
• In main memory portion will allocated for operating system(low
memory) and the remain allocate to the process (held in high
memory).
• If all partitions are occupied, the operating system can swap a process out
of a partition.
24
Cont…
i. Equal-size partitions
• If there is an available partition, a process can be loaded into that
partition because all partitions are of equal size, it does not matter
which partition is used.
o If all partitions are occupied by blocked processes, choose one
process to swap out to make room for the new process:- The
operating system can swap a process out of a partition If none are in
a ready or running state.
0 0
OS OS
400K 400K
P5 P5
900K 100K 900K
1000K
P4 P4
1700K 300K 1600K
2000K Compact 1900K P3
P3
2300K 660K
260K
2560K 2560K
27
Cont…
• External fragmentation happens when there’s a sufficient quantity
of area within the memory to satisfy the memory request of a
method.
28
Memory allocation (Placement) algorithm
• When new process is created or swapped in operating system must
decide which free block to allocate to a process.
• Goal: to reduce usage of compaction (time consuming).
• Possible Algorithms to allocate free space for newly created
process are:-
First-fit algorithm
o Scans memory form the beginning and chooses the first
available block that is large enough
o Simple and Fastest algorithm
Next-fit
o Begins to scan memory from the location of the last
placement, and chooses the next available block that is large
enough.
o More often allocate a block of memory at the end of memory
where the largest block is found.
29
Cont…
• Best-fit algorithm
o Chooses the block that is closest in size to the request
o Since smallest block is found for process, the smallest
amount of fragmentation is left
o Memory compaction must be done more often
• Worst-fit algorithm
o Scan all hole for largest available hole
o Choose the largest hole; must also search entire list
o Produces the largest waste hole
30
Paging and segmentation
• Paging is a technique in which physical memory is broken into
blocks of the same size called pages.
32
Cont…
• For implementing paging the physical and logical memory
spaces are divided into the same fixed sized blocks.
33
Cont…
o Page number (p) – used as an index into a page table which contains
base address of each page in physical memory
o Page offset (d) – combined with base address to define the physical
memory address that is sent to the memory unit.
• The page table uses page number as an index; each process has its
separate page table that maps logical address to the physical address.
• The page table contains base address of the page stored in the frame
of physical memory space.
• The base address defined by page table is combined with the page
offset to define the frame number in physical memory where the
page is stored.
• Operating system maintains a page table for each process
o Contains the frame location for each page in the process
o Memory address consist of a page number and offset within the page
34
Implementation of Page Table
• Page table is kept in main memory
37
Cont…
The optimal Page replacement algorithm
• It is easy to describe but impossible to implement.
• At the moment the page fault occurs each label can be
labelled with number of instruction that will be
executed first before that page is referenced
• Page with highest label will be removes
38
Cont…
The not Recently use Page replacement algorithm
• OS is responsible to collects statics about which page are being used
• Most computers contain virtual memory with two status bits
R- is set whenever the page is referenced
M- is set whenever the page is modified
• OS divides all pages into four categories based on the current
values of their R and M bits.
Class 0: not referenced, not modified
Class 1: not referenced, modified
Class 2: referenced, not modified
Class 3: referenced, modified
• NRU (Not Recently Used) algorithm removes a page at random
from the lowest
39
Cont…
FIFO page replacement algorithm
40
Cont…
The Second Chance Page Replacement
Algorithm
• Avoids the problem of throwing out a heavily used page
is to inspect the R bit of the oldest page
• If it is 0, the page is both old and unused, so it is
replaced immediately.
• If the R bit is 1, the bit is cleared, the page is put onto
the end of the list of pages
• Then the search continues.
41
Cont…
The Clock Page Replacement Algorithm
42
Cont…
Least Recently Used Page
• Assume pages used recently will used again
• It through the page has been unused for long period
of time
• Keeps linked list of pages on the time they had
accessed lastly
43
Cont..
44
Virtual memory
• Virtual memory – A technique that allows a process to execute in the main
memory space which is smaller than the process size.
• Virtual memory is a memory management capability of OS that uses
hardware and software to allow a computer to compensate for physical
memory shortages by temporarily transferring data from RAM to disk
storage.
• Modern operating system memory management technique is virtual
memory.
• Virtual memory provides many benefits:
Only part of the program needs to be in memory for execution
Logical address space can be much larger than physical address
space
Allows address spaces to be shared by several processes
Allows for more efficient process creation
Higher transfer bandwidth :CPU can do other things during transfer
faster,
More consistent response times (no interrupt or polling overhead)
45
Direct Memory Access (DMA)
• DMA refers to the ability of an I/O device to transfer data directly
to and from memory without going through the CPU.
• It mean that it is a method that allows an I/O device to send or
receive data directly to or from the main memory, by passing the
CPU to speed up memory operations.
• The process is managed by a chip known as a DMA controller
(DMAC).
• DMA controller performs functions that would be normally carried
out by the processor: For each word, it provides the memory
address and all the control signals.
• To transfer a block of data, it increments the memory addresses and
keeps track of the number of transfers.
• DMA controller can transfer a block of data from an external
device to the processor, without any intervention from the
processor.
46
Working Set and Trash
• The working set is stands for parts of memory that the current algorithm
is using and is determined by which parts of memory the CPU just
happens to access.
• If it is small enough, it can all fit in the cache and your algorithm will run
very fast.
• On the OS level, the kernel has to tell the CPU where to find the physical
memory your application is using every time you access a new page so
also you want to avoid that hit as much as possible.
• The set of pages that a process is currently using is its working set
• If the entire working set is in memory
o Process will run without causing many faults 47
Cont…
• If the available memory is too small to hold the entire
working set.
o Process will cause many page faults
o Run slowly
• A program causing page faults every few instructions is
said to be thrashing
48
Cache
• Fast memory placed between the CPU and main memory.
• When some data is requested, the cache is first checked to see
whether it contains that data.
o If data is already in the cache, it is called a cache hit, then the data
can be retrieved from the cache, which is much faster than retrieving
it from the original storage location.
o If the requested data is not in the cache, it is called a cache miss, then
the data needs to be fetched from the original storage location to
cache, which would take a longer time.
50
.
51