Memory Management
Memory Management
MEMORY MANAGEMENT
Definition
Since memory space is generally a limited resource of a computer, it must
be shared by different programs. Thus, how the operating system manages
this limited resource, the memory space, for the loading and execution of
its own and user program code is referred to as memory management.
The part of the OS that manages the memory hierarchy is called memory
manager.
In a multiprogramming systems memory must be subdivided to
accommodate multiple processes. Memory needs to be allocated to ensure
a reasonable supply of ready processes to consume available processor
time
Memory Management Techniques
It is divided into 2 classes:
i. The entire process that reside in the memory throughout execution
i.e. without swapping
ii. Those that move processes back and forth between main memory
and disk during execution i.e. swapping.
Objectives of memory management
i. Relocation
Page 1 of 26
Operating System
Programmer does not know where the program will be placed in memory
when it is executed. While the program is executing, it may be swapped
Page 2 of 26
Operating System
ii. Protection
Each process should be protected against unwanted interference by other
processes. Processes should not be able to reference memory locations in
another process without permission. Because the location of a program in
main memory is unpredictable, it is Impossible to check absolute
addresses at compile time to assure protection. Memory protection
requirement must be satisfied by the processor (hardware) rather than the
operating system (software). Operating system cannot anticipate all of the
memory references a program will make
iii. Sharing
Any protection mechanism must have the flexibility to allow several
processes to access the same portion of memory. If a number of processes
are executing the same program it’s better to allow each process access to
the same copy of the program rather than have their own separate copy
iv. Logical Organization
Programs are written in modules and these modules can be written and
compiled independently. Different degrees of protection given to modules
(read-only, execute-only)
Share modules among processes
v. Physical Organization
Memory available for a program plus its data may be insufficient.
Overlaying allows various modules to be assigned the same region of
memory since programmer does not know how much space will be
available.
Functions of memory management
Page 3 of 26
Operating System
Page 4 of 26
Operating System
TERMINOLOGIES
Page 5 of 26
Operating System
Page 6 of 26
Operating System
Registers are the top of the memory hierarchy and are the fastest
way for the system to manipulate data. Below them are several
levels of cache memory, at least some of which is also built into the
CPU and some of which might be on other, dedicated chips. Cache
memory is slower than registers but much more abundant. Below
the various levels of cache is the main memory, which is even slower
but vastly more abundant (e.g., hundreds of megabytes as compared
with only 32 registers). But it, in turn, is still far faster and much less
capacious than storage devices and media (e.g., hard disk drives and
CD-ROMs).
Types of registers
Registers can be classified into
i. General purpose:
As temporary holding places for data that is being
manipulated by the CPU. That is, they hold the inputs to the
arithmetic/logic circuitry and store the results produced by that
circuitry.
ii. Special purpose types:
Special purpose registers store internal CPU data, such as
the program counter (also termed instruction pointer), stack
pointer and status register. Program counters contain the
address of the next instruction to be executed. Instruction
registers hold the instruction being executed by the CPU,
address registers hold memory addresses and are used to
access memory, and data registers are used to store integers.
Others include:
Fixed partition
Page 8 of 26
Operating System
Page 9 of 26
Operating System
Simple segmentation
Each process is divided into a number of segments. A process is
loaded by loading all of its segments into dynamic partitions that
need not to be continuous.
Advantages
i. No internal fragmentation
ii. Improved memory utilization and reduced overhead
compared to dynamic partitioning
Disadvantaged
i. External fragmentation
Virtual memory
As with simple paging except that it is not necessary to load all of
the pages of a process. Nonresident pages that needed are brought
in later automatically.
Advantages
i. No external fragmentation
ii. Higher degree of multiprogramming
iii. Large virtual address space
Disadvantage
i. Overhead of complex memory management
Virtual memory segments
As with simple segmentation, except that it is not necessary to load
all of the segments of a process. Nonresident segments that are
needed are brought in latter automatically
Advantages
i. No internal fragmentation
ii. Higher degree of multiprogramming
iii. Large virtual address space
iv. Protection and sharing support
Disadvantage
Page 10 of 26
Operating System
User
OS in User program
RAM program OS in
RAM
The portion of the system in the ROM is called the BIOS (Basic
Input Output System). When a system is organized in this way, only
one process at a time can be running. As soon as the user types a
Page 11 of 26
Operating System
Page 12 of 26
Operating System
Page 13 of 26
Operating System
Page 14 of 26
Operating System
SWAPPING
With time sharing systems sometimes there is not enough main memory
to hold all the current active process, so excess process must be kept on
the disk and brought into run dynamically.
Two general memory management techniques can be sued:
i. Swapping
Consists of bringing in each process in its entirely, running it for a
while, then putting it back onto the disk.
ii. Virtual Memory
Allows programs to run even when they are only partially in main
memory for example initially only process A is in memory. Then
process B and C are created or swapped in from disk. A terminates or
is swapped out to disk. Then D comes in and B goes out. Finally E
comes in.
C B C C
C
B C B
B E
A
A A D
D D
OS OS OS OS OS
OS OS
Page 16 of 26
Operating System
Physical Memory
Program address
B Base Register
A
B+a
C Limit register
Page 17 of 26
Operating System
Disadvantages
i. Increase hardware cost
ii. Increase overheads for handling paging interrupts
iii. Increase software completely to prevent thrashing
(b) Paging
This technique makes use of fixed-length blocks called pages and assigns
them to fixed regions of physical memory called page frames. Each
process is subdivided into pages. Each logical address consists of two
parts: a page address and a displacement (line address). The memory map
(page table) contains the information. For each (logical) page address,
there is a corresponding (physical) address of a page frame in main or
secondary memory.
When the presence bit P is I, the page in question is present in main
memory, and the page table contains the base address of the page frame
to which the page has been assigned. If P=O, a page fault occurs, and a
page swap ensures. The change bit C is used to specify whether or not the
page has been changed since last being loaded into main memory. If a
Page 18 of 26
Operating System
Paging leads to better space utilization and this improves the system
throughput.
Process A Process B A1
PAGE 1 PAGE 1 A2
A3
2 2
A4
3 3
A5
4
B1
5
B2
Process Structure B3
Memory space
(c) Segmentation
It divides a process by using variable length chunks, called
segments. A segment is a set of logically related contiguous words.
Thus allocates memory by segments. When a segment not currently
resident in main memory is required, the entire segment is
transferred from secondary memory. The physical addresses
assigned to the segments are maintained in a memory map called a
segment table, which may itself be a re-locatable segment. Main
Page 19 of 26
Operating System
Advantages
i. Facilitate use of dynamic memory
ii. Facilitate sharing of code & data between processes
iii. Logical structure of the process reflected in the physical
structure which reinforced the locality principle
Others
1. Swapping - (single system process allocation)
When a new process wants to be executed, the process in memory
will be transferred to secondary store and the new process will be
placed into main memory, thus being a ‘swap’ of two processes,
hence called swapping. Swapping systems do not usually employ
the standard file management facilities but an area of disk space is
dedicated to the swapping activity and special software manages the
data transfer at high speed.
Page 20 of 26
Operating System
2. Partitioned Allocations
They are of two types.
Fixed partition
Memory is divided into a number of separate fixed areas, each of
which can hold one process. The number of partitions would be
controllable, within limits, by the system manager and would
depend on the amount of memory available & the size of processes
to be run. Each partition will typically contain unused space-
internal fragmentation. The partition will be of different sizes so that
a mixture of large & small processes can be accommodated.
Variable partition
In this, the partitions are not fixed but vary depending on the
memory space that is present. Sometimes this results in external
fragmentation. Variable partition allocation with compaction
involves moving the process about the memory in order to close up
the holes and hence bring the free space into a single large block.
Page 21 of 26
Operating System
3. Overlays
The essence of overlays is that the object program is constructed as
a number of separate modules called overlays which can be loaded
individually and selectively into the same area of memory. The
object program consists of a root section which is always in memory
and two or more loadable overlays. Only one overlay will be present
in memory at any time. The whole system has to be managed at the
program level and care has to be taken to avoid frequent switching
of overlays. This was unique to MS Dos.
Page 22 of 26
Operating System
Page 23 of 26
Operating System
Page 24 of 26
Operating System
Page 25 of 26
Operating System
Segmentation Paging
1. Segments varies in size 1. Pages are of fixed sizes
2. Programmer is aware of 2. Programmer is not aware of
segmentation paging
3. There are many address spaces 3. There is one linear address
space
4. Procedures and data can be 4. Data / procedures cannot be
distinguished and separately separately protected.
protected.
5. There is no overflow from the 5. There is automatic overflow
end of a segment. from the end of one page to the
next.
6. Sharing of procedures is 6. Sharing of procedures is not
facilitated facilitated.
7. When segments becomes large, 7. A page will fit in any page
there’s difficulty to find a block of frame.
memory big enough for them to fit
in.
Page 26 of 26