OS - Chapter - 4 - Memory Management
OS - Chapter - 4 - Memory Management
Meti Dejene
[email protected]
Haramaya University
Chapter 4 – Memory Management
Introduction
Memory management mainly deals with how operating systems create abstractions from
memory and how they manage them.
a few terabytes of slow, cheap, nonvolatile magnetic or solid-state disk storage, not to
mention removable storage, such as DVDs and USB sticks.
It is the job of the operating system to abstract this hierarchy into a useful model and then
manage the abstraction.
Cont.
The part of the operating system that manages the memory hierarchy is called the memory
manager.
There are several different memory management models, ranging from very simple to
highly sophisticated.
We will focus on the programmer’s model of main memory and how it can be managed.
No Memory Abstraction Scheme
Early mainframe computers (before 1960), early minicomputers (before 1970), and
early personal computers (before 1980) had no memory abstraction.
MOV REGISTER1,1000
the computer just move the contents of physical memory location 1000 to REGISTER1.
Thus, the model of memory presented to the programmer was simply physical memory, a
set of addresses from 0 to some maximum.
Cont.
In these conditions, it was not possible to have two running user programs in memory at
the same time.
Overall, when the system is organized in this way, generally only one user process at a
time can be running.
As soon as the user types a command, the operating system copies the requested
program from disk to memory and executes it.
When the process finishes, the operating system displays a prompt character and
waits for a user new command.
When the operating system receives the command, it loads a new program into
memory, overwriting the first one.
Cont.
One way to get some parallelism in a system with no memory abstraction is to program
with multiple threads.
This is possible because all threads in a process are supposed to see the same
memory image.
However, the problem is what people often want is unrelated programs to be running
at the same time, something the threads abstraction does not provide.
Running Programs Without a Memory Abstraction
Option 1
The operating system has to save the entire contents of memory to a disk file, then bring in
and run the next program.
As long as there is only one program at a time in memory, there are no conflicts when
memory referencing instructions are executed.
Cont.
Option 2
With the addition of some special hardware (as in the early models of the IBM 360)
In this approach, Memory was divided into 2-KB blocks and each was assigned a 4-bit
protection key held in special registers inside the processor.
So, a machine with a 1-MB memory needed only 512 of these 4-bit registers for a total
of 256 bytes of key storage.
Any attempt by a running process to access memory with a protection code different
from its own is forbidden.
Since only the operating system could change the protection keys, user processes were
prevented from interfering with one another and with the operating system itself.
Cont.
Nevertheless, this solution had a major drawback.
For example, if we have two programs, each 16 KB in size, as shown in Fig. 3-2(a) and (b).
The first program starts out by jumping to address 24, which contains a MOV
instruction.
The second program starts out by jumping to address 28, which contains a CMP
instruction.
When the two programs are loaded consecutively in memory starting at address 0, we
have the situation of Fig. 3-2(c).
Since they have different memory keys, neither one can damage the other.
Cont.
But the problem is when the first program starts, it executes the JMP 24 instruction, which
jumps to the instruction, as expected. This program functions normally.
However, after the first program has run long enough, the operating system may decide
to run the second program, which has been loaded above the first one, at address
16,384.
The first instruction executed is JMP 28, which jumps to the ADD instruction in the first
program, instead of the CMP instruction it is supposed to jump to. The program will most
likely crash. Why?
So, a better solution is to invent a new abstraction for memory: the address space.
The notion of address space is to create a kind of abstract memory for programs to live in.
An address space is the set of addresses that a process can use to address memory.
Each process has its own some set of address space it can use, typically running from 0
up to some maximum, independent of those belonging to other processes.
Determines the legal addresses that the process can access legally
The classical solution is to equip CPU with two special hardware registers, usually called the
base and limit registers.
the base register is loaded with the physical address where its program begins in
memory and
the CPU hardware automatically adds the value in the base register to the address
generated by the user process before sending the address out on the memory bus.
This whole technique of the operating system, to load a program into memory at any
available location and adjust the program's memory references accordingly during
runtime is what we call Dynamic relocation.
Programs are loaded into memory locations wherever there is room and without
relocation during loading.
In short, what Dynamic relocation does is map each process’ address space onto a
different part of physical memory in a simple way.
Cont.
In such a way,
1. A pair of base and limit registers, define the range of legal addresses that a process
may access.
Every address generated during relocation must fall within the range specified by
the limit register.
A trap is generated for any attempt by a user process to access beyond the limit.
Based on Fig. 3-2(c), the base and limit values that would be loaded into these hardware
registers when the first program is run are 0 and 16,384, respectively.
The values used when the second program is run are 16,384 and 32,768, respectively.
If a third 16-KB program were loaded directly above the second one and run, the base
and limit registers would be 32,768 and 16,384.
Thus, in the case of the first instruction of the second program in Fig. 3-2(c), the process
executes a
JMP 28
instruction, but the hardware treats it as though it were
JMP 16412
so it lands on the CMP instruction as expected.
Cont.
So, using base and limit registers is an easy way to give each process its own private
address space because every memory address generated automatically has the base-
In practice, the total amount of RAM needed by all the processes is often much more
than what can fit in memory.
Consequently, keeping all processes in memory all the time requires a huge amount of
memory and cannot be done if there is insufficient memory.
Two general approaches to deal with memory overload have been developed over the
years.
Swapping is required
External fragmentation: arises when total memory space is enough to satisfy a request or
Internal fragmentation: occurs when memory blocks allocated to processes are larger
than necessary, and the allocated space isn't fully utilized resulting in unused wasted
When this happens it is possible to shuffle memory contents by moving all processes
toward one end of the memory so as to place all free memory together in one large block
1. If processes are created with a fixed size that never changes, then the allocation is simple:
the operating system allocates exactly what is needed, no more and no less.
2. If, however, processes’ data segments can grow, a problem might occur whenever a
process tries to grow.
Therefore, to avoid this, if it is expected that processes will grow as they run,
The operating systems way to keep track of assigned and free memory.
With a bitmap, memory is divided into allocation units as small as a few words and
Corresponding to each allocation unit is a bit in the bitmap, which is 0 if the unit is
Each entry in the list specifies a hole (H) or process (P), the address at which it starts, the
These may be either processes or holes, leading to the four combinations shown in Fig.
3-7.
Memory Allocation Algorithms
Several algorithms can be used to allocate memory for a created process or an existing
process being swapped in from disk.
I. First fit
The memory manager scans along the memory units until it finds a hole that is big
enough.
It works the same way as first fit, except that it keeps track of where it is whenever it
finds a suitable hole.
The next time it is called to find a hole, it starts searching the list from the place
where it left off last time, instead of always at the beginning, as first fit does.
Cont.
III. Best fit
Searches the entire memory, from beginning to end, and takes the smallest hole that is
adequate enough a given process.
Rather than breaking up a big hole that might be needed later, best fit tries to find a hole
that is close to the actual size needed, to best match the request and the available holes.
always take the largest available hole, so that the new hole will be big enough to be
useful later.
V. Quick fit
Maintains separate lists for some of the more common sizes requested.
Quiz - 1
1. Which memory allocation algorithm searches the entire memory from
beginning to end and allocates the smallest hole that is adequate enough
for a process?
2. What are the 2 methods used by the operating system to keep track of
allocated and free memory units?
These partitions remain static and do not change in size during execution of the system.
Disadvantages: (1) Potential for internal fragmentation If a process is smaller than the
partition size and (2) inefficient utilization of memory as smaller processes might occupy
larger partitions, limiting the number of processes that can be accommodated
simultaneously.
Fixed partitioning Scheme
In here, memory is allocated and deallocated based on the actual memory requirements
of processes.
It involves dividing memory into variable-sized partitions that can change dynamically
space to be noncontiguous.
The basic idea behind paging is that each program has its own address space, which is
Each page is a contiguous range of addresses and has assigned page number.
Physical memory is broken up into fixed-sized blocks called frames or page frames.
When a process is to be executed, its pages are mapped and loaded into any
The page table shows which page is in which frame and it contains base address of
The page number is used as an index into the page table, yielding the number
From the page table entry, the page frame number (if any) is found.
Looking up the mapping in the page table hierarchy is known as a page table walk.
Structure of a Page Table Entry
Cont.
Protection bits
tell what kinds of access are permitted (with 0 for read/write and 1 for read only).
If the page has been modified (i.e., is ‘‘dirty’’), it must be written back to the disk.
If it has not been modified (i.e., is ‘‘clean’’), it can just be abandoned, since the disk
copy is still valid.
Referenced bit: Set whenever a page is referenced, either for reading or for writing.
used to help the operating system choose a page to evict when a page fault occurs.
Pages that are not being used are better candidates than pages that are, and this bit
plays an important role in page replacement algorithms.
Cache disable bit allows caching to be disabled or enabled for the page.
Cont.
A Present/absent bit indicates if a page is physically present in the main memory i.e.
mapped or not.
picks a page frame to evict and writes its contents back to the disk,
then fetches and maps the page that was just referenced into the page frame just
freed, changes the map, and restarts instruction.
Virtual Memory
While memory sizes are increasing rapidly, software sizes are increasing much faster.
As a consequence, there is a need to run programs that are too large to fit in memory.
This disk space designated for this purpose is called virtual memory and it has similar
structure with RAM.
Addresses in virtual memory are called virtual addresses and form the virtual address
space.
How Virtual Memory Works
Implementation of Virtual Memory is achieved using the technique of Paging.
Large programs are broken down into pages and stored on an area designated as
Virtual memory.
Then depending on the free space available on the main memory, pages from the
virtual memory are mapped onto the physical memory frames for execution.
The rest will await in the virtual memory until they are required for execution.
MMU (Memory Management Unit) maps the virtual addresses onto the physical
memory addresses.
This approach allows execution of process which are not completely in main memory.
Logical implementation of Virtual Memory
Segmentation
A given program layout has different sections (the executable program code, program
For example, a code segment may contain the actual code instructions, a data
segment may store initialized data, and a stack segment may handle function calls
Segment Number