Operating System
Lecture 12
Memory Management
Background
Swapping
Contiguous Memory Allocation
Fragmentation
Types of Fragmentation
Segmentation
Base and Limit Registers
A pair of base and limit registers define the logical address space
CPU must check every memory access generated in user mode to be sure it is between base and limit
for that user
Hardware Address Protection
Logical vs. Physical Address Space
The concept of a logical address space that is bound to a separate physical address space is
central to proper memory management
Logical address – generated by the CPU; also referred to as virtual address
Physical address – address seen by the memory unit
Logical address space is the set of all logical addresses generated by a program
Physical address space is the set of all physical addresses generated by a program
Swapping
A process can be swapped temporarily out of memory to a backing store, and then brought back
into memory for continued execution
Total physical memory space of processes can exceed physical memory
Backing store – fast disk large enough to accommodate copies of all memory images for all
users; must provide direct access to these memory images
Roll out, roll in – swapping variant used for priority-based scheduling algorithms; lower-
priority process is swapped out so higher-priority process can be loaded and executed
Major part of swap time is transfer time; total transfer time is directly proportional to the
amount of memory swapped
System maintains a ready queue of ready-to-run processes which have memory images on disk
Swapping (Cont.)
Does the swapped out process need to swap back in to same physical addresses?
Depends on pending I/O to and from process memory space
Modified versions of swapping are found on many systems (i.e., UNIX, Linux, and Windows)
Swapping normally disabled
Started if more than threshold amount of memory allocated
Disabled again once memory demand reduced below threshold
Schematic View of Swapping
Context Switch Time including Swapping
If next processes to be put on CPU is not in memory, need to swap out a process and swap in
target process
Context switch time can then be very high
100MB process swapping to hard disk with transfer rate of 50MB/sec
Swap out time of 2000 ms
Plus swap in of same sized process
Total context switch swapping component time of 4000ms (4 seconds)
Can reduce if reduce size of memory swapped – by knowing how much memory really being
used
Context Switch Time and Swapping (Cont.)
Other constraints as well on swapping
Pending I/O – can’t swap out as I/O would occur to wrong process
Or always transfer I/O to kernel space, then to I/O device
Known as double buffering, adds overhead
Contiguous Allocation
Main memory must support both OS and user processes
Limited resource, must allocate efficiently
Contiguous allocation is one early method
Main memory usually into two partitions:
Resident operating system, usually held in low memory with interrupt vector
User processes then held in high memory
Each process contained in single contiguous section of memory
Contiguous Allocation (Cont.)
Relocation registers used to protect user processes from each other, and from changing operating-
system code and data
Base register contains value of smallest physical address
Limit register contains range of logical addresses – each logical address must be less than the limit register
Multiple-partition allocation
Multiple-partition allocation
Degree of multiprogramming limited by number of partitions
Variable-partition sizes for efficiency (sized to a given process’ needs)
Free Partition(Hole) – block of available memory; holes of various size are scattered throughout 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)
Dynamic Storage-Allocation Problem
How to satisfy a request of size n from a list of free holes?
First-fit: Allocate the first hole that is big enough
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
First-fit and best-fit better than worst-fit in terms of speed and storage utilization
Fragmentation
A process(s) are loaded and removed from memory. The free memory space is broken into
little pieces.
After some time that process(s) can not be allocated to memory because of small size and
memory block remain unused. This problem is called Fragmentation
Fragmentation
External Fragmentation – total memory space exists to satisfy a request to reside a process
in it, but it is not contiguous.
Internal Fragmentation – Memory block assign to a process is bigger and some portion of
memory is left unused as it cannot be used by another process.
First fit analysis reveals that given N blocks allocated, 0.5 N blocks lost to fragmentation
Fragmentation (Cont.)
Reduce external fragmentation by compaction
Shuffle memory contents to place all free memory together in one large block
Compaction is possible only if relocation is dynamic, and is done at execution time
External Fragmentation can be reduced through compaction and shuffle free memory together
in one large block.
Internal Fragmentation can be reduced by assigning the smallest partition to large enough for
the process.
Segmentation
What is segmentation?
Segmentation is a memory management technique in which we divide the
process into smaller segments. The process is segmented module by module. In
main memory, we only store the segments of the process. Process segment table is
used to keep the record of segments, its size, and its memory address
Segmentation
What are advantages of segmentation?
The segment table is used to keep the record of segments and segment table occupies less space as
compared to the paging table.
No internal fragmentation
What are disadvantages of segmentation?
Due to segments external fragmentation occurs and external fragmentation results in a lot of memory
waste.
Logical View of Segmentation
4
1
3 2
4
user space physical memory space
Segmentation Architecture
Segment table – maps two-dimensional physical addresses; 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
Segment Size and Address
In this example, the process is divided into three segments.
Size of segment 1 is 50KB
Segment 2 is of 100KB
Segment 3 is of 60KB.
Segment 1 is stored on address 11
Segment 2 is stored on address 13
Segment 3 is stored on address 14