You are on page 1of 44

Chapter 3

Memory Management
Outline

• Introduction to Memory Management


• Memory Management Requirements
• Memory Partitioning
• Basic blocks of memory management
– Paging
– Segmentation
Introduction
• Ideal - an infinitely large, inexpensive, fast memory that is also nonvolatile.
– Technology does not provide for such.
• The memory hierarchy is based on storage capacity, speed, and cost.
• Lower in the hierarchy, storage capacities have lesser speed and cost.
• Most computers have a memory hierarchy, with
– a small amount of very fast, inexpensive, volatile cache memory,
– some number of megabytes of medium-speed, medium-price volatile main
memory (RAM)
– hundreds or thousands of megabytes of slow, cheap, non-volatile disk
storage.
• It is the work of the operating systems to co-ordinate how these memories
are used
Introduction (cont)

• Memory manager - part of the operating system that manages the


memory hierarchy
– Its keeps track of which parts of memory are in use and which parts are not in
use,
– It allocates memory to processes when they need it and de-allocate it when
they are done.
– It also manages swapping between main memory and disk when main
memory is not enough to hold all the processes
Memory Management

Memory needs to be allocated to ensure


a reasonable supply of ready processes
to consume available processor time
Memory Management
Requirements
• Introduction
• Relocation
• Protection
• Sharing
• Logical organisation
• Physical organisation
Requirements: Relocation

• The programmer does not know where


the program will be placed in memory
when it is executed,
– it may be swapped to disk and return to
main memory at a different location
(relocated)
• Memory references must be translated
to the actual physical memory address
Relocation

• When program is loaded into memory the


actual (absolute) memory locations are
determined
• A process may occupy different partitions
which means different absolute memory
locations during execution
– Swapping
– Compaction
Memory Addressing

• A logical address is simply an integer


value relative to the starting point of the
program
• Physical Address - An actual address in the
main memory device
• To produce a physical address, we add a
logical address to the starting address
of the program in physical main memory
Single Contiguous Memory
Management

binding a logical address


to a physical one
Registers Used during
Execution
• Base register
– Starting address for the process

• Bounds register
– Ending location of the process

• These values are set when the process


is loaded or when the process is
swapped in
Registers Used during
Execution
• The value of the base register is added
to a relative address to produce an
absolute address
• The resulting address is compared with
the value in the bounds register
• If the address is not within bounds, an
interrupt is generated to the operating
system
Relocation - Memory
Management Terms
Term Description
Frame Fixed-length block of main
memory.
Page Fixed-length block of data in
secondary memory (e.g. on
disk).
Segment Variable-length block of data that
resides in secondary memory.
Requirements: Sharing

• Allow several processes to access the


same portion of memory
• Better to allow each process access to
the same copy of the program rather
than have their own separate copy
Requirements: Logical
Organization
• Memory is organized linearly (usually)
• Programs are written in modules
– Modules can be written and compiled
independently
• Different degrees of protection given to
modules (read-only, execute-only)
• Share modules among processes
• Segmentation helps here
Requirements: Physical
Organization
• Cannot leave the programmer with the
responsibility to manage memory
• Memory available for a program plus its data
may be insufficient
– Overlaying allows various modules to be
assigned the same region of memory but is time
consuming to program
• Programmer does not know how much
space will be available
Memory Partitioning

• An early method of managing memory


– Pre-virtual memory
– Not used much now

• However
– Virtual Memory has evolved from the
partitioning methods
Types of Partitioning

• Fixed Partitioning
• Dynamic Partitioning
• Paging
• Dynic Paging
• Segmentation
Fixed Partitioning

• Equal-size partitions
– Any process whose size is less
than or equal to the partition size
can be loaded into an available
partition
• The operating system can swap a
process out of a partition
– If none are in a ready or running
state
Fixed Partition Selection

• First fit
– Program is allocated to the first partition big enough to hold it
• Next fit
– works the same way as first fit, except that it keeps track of
where it is whenever it finds a suitable partition
• Best fit
– Program is allocated to the smallest partition big enough to
hold it
• Worst fit
– Program is allocated to the largest partition big enough to
hold it
Fixed Partitioning Problems

• A program may not fit in a partition.


– The programmer must design the program
with overlays
• Main memory use is inefficient.
– Any program, no matter how small, occupies
an entire partition.
– This is results in internal fragmentation.
Solution – Unequal Size
Partitions
• Lessens both problems
– but doesn’t solve completely

• In the figure on the right,


– Programs up to 16M can be
accommodated without overlay
– Smaller programs can be placed in
smaller partitions, reducing internal
fragmentation
Placement Algorithm

• Equal-size
– Placement is trivial (no options)

• Unequal-size
– Can assign each process to the smallest
partition within which it will fit
– Queue for each partition
– Processes are assigned in such a way as to
minimize wasted memory within a partition
Fixed Partitioning

Memory Assignment for Fixed Partitioning


Remaining Problems with
Fixed Partitions
• The number of active processes is limited
by the system
– That is, limited by the pre-determined
number of partitions
• A large number of very small processes
will not use the space efficiently
– In either fixed or variable length partition
methods
Dynamic Partitioning

• Partitions are of variable length and


number
• Dynamic partitions Partitions are
created to fit the needs of the programs
• Process is allocated exactly as much
memory as required
Dynamic Partitioning

• Operating system must decide which free


block to allocate to a process
• Best-fit algorithm
– Chooses the block that is closest in size to
the request
– Worst performer overall
– Since smallest block is found for process, the
smallest amount of fragmentation is left
– Memory compaction must be done more often
Dynamic Partitioning

• First-fit algorithm
– Scans memory from the beginning and
chooses the first available block that is
large enough
– Fastest
– May have many process loaded in the front
end of memory that must be searched over
when trying to find a free block
Dynamic Partitioning

• Next-fit
– Scans memory from the location of the last
placement
– More often allocate a block of memory at the
end of memory where the largest block is found
– The largest block of memory is broken up
into smaller blocks
– Compaction is required to obtain a large block
at the end of memory
Allocation Schemes

Example of memory configuration before and after allocation of 16M block


Paging

• Partition memory into small equal fixed-


size chunks and divide each process
into the same size chunks
• The chunks of a process are called
pages
• The chunks of memory are called frames
FRAGMENTATION

All the preceding algorithms suffer from: External Fragmentation


As processes are loaded and removed from memory the free memory
is broken into little pieces and enough total space exists to satisfy a
request, but it is not contiguous.

Solutions:
•Break memory into fixed-sized blocks and allocate in units of block
sizes. Since the allocation will always be slightly larger than the
process, some Internal Fragmentation still results.
•Compaction: move all processes to one end of memory and holes to the
other end. Expensive and can only be done when relocation is done at
execution time, not at load time.
Paging

• The operating system maintains a


separate page-map table (PMT) for each
process in memory
– Contains the frame location for each page
in the process
– Memory address consist of a page number
and offset within the page
Paged Memory Management

• An important extension is demand paging


– not all parts of a program actually have to
be in memory at the same time
– In demand paging, the pages are brought
into memory on demand
– The act of bringing in a page from secondary
memory, which often causes another page to
be written back to secondary memory, is
called a page swap
Paged Memory Management

• The demand paging approach gives rise to


the idea of virtual memory, the illusion
that there are no restrictions on the size of
a program
• Too much page swapping, however, is
called thrashing and can seriously
degrade system performance.
Page Replacement Algorithms

• If new page is brought in, need to chose a page


to evict
• Don’t want to evict heavily used pages
• If page has been written to, need to copy it to
disk.
• Otherwise, a good copy is on the disk=>can
write over it
Optimal Page Replacement Algorithm

• Each page can be labeled with the number of instructions that


will be executed before the page is first referenced
• Page with the highest label should be removed.
• Problem with this algorithm is that it is not realizable.
• At the point of the page fault the OS has no idea of when each of
the pages will be referenced next.
• By running a program on a simulator and keeping track of all page
references, it is possible to implement optimal page replacement
algorithm on the second run by using the page reference
information collected during the first run.
• This method is not used in practical systems
Not Recently Used (NRU)
Algorithm
• In most computers with virtual memory - two status bits associated with each
page for collecting useful statistics.
– R is set when the page is referenced (read)
– M when the page is written to (modified).

• Once a bit has been set to 1, it will stay until the operating system resets it to 0.
•  When a process is started up, both page bits for all its pages are set to 0 by
OS.
• When a page fault occurs, OS inspects all the pages and divides them into four
categories based on the current values of the 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 algorithm removes a page at random from the lowest numbered non-
empty class.
First In First Out (FIFO)
Algorithm
• The operating system maintains a list of all
pages currently in memory, with the page at the
head of the list the oldest one and the page
at the tail the most recent arrival.
• On a page fault, the page at the head is
removed and the new page added to the tail
of the list.
• Not a good algorithm because there are
chances of removing a heavily used page
Second Chance Algorithm
• Simple modification to FIFO that avoids the problem of throwing out a
heavily used page
• Inspect the R bit of the oldest page
– If it is 0, the page is both old and unused, so it is replaced.
– If the R bit is 1, the bit is cleared and the page is put at the end of the list of pages,
and its load time is updated as though it has just arrived in memory.
• The search continues.
• The second chance looks for an old page that has not been referenced in the
previous clock interval.
• If all have been referenced then second chance degenerates into a pure FIFO.
• Suppose that all the pages have their R bits set.
– One by one the OS moves the pages to the end of the list and clears their R bit.
– Eventually, it comes back to page A, which now has its R bit cleared.
– At this point A is evicted. Thus the algorithm always terminates.
Least Recently Used (LRU) Algorithm

• Pages that have been heavily used in the last few instructions will
probably be heavily used again in the next few instructions.
• Conversely, pages that have not been used for ages will probably
remain unused for a longer time.
• When page fault occurs, OS throws out the pages that have been
unused for the longest time.
• It is necessary to maintain a linked list of all pages in memory, with the
most recently used page at the front and the least recently used
page at the rear.
• The problem is that the list must be updated on every memory
reference.
• Finding a page in the list, deleting and then moving it from to the front is
a very time consuming operation.
Segmentation

• A program can be subdivided into segments


– Segments may vary in length
– There is a maximum segment length

• Addressing consist of two parts


– a segment number and
– an offset

• Segmentation is similar to dynamic


partitioning
Advantages of Segmentation
• Simplifies handling of data structures which are
growing and shrinking
• Address space of segment n is of form (n,local
address) where (n,0) is starting address
• Can compile segments separately from other
segments
• Can put library in a segment and share it
• Can have different protections (r,w,x) for different
segments
Paging vs Segmentation

You might also like