Memory Management
Memory Management
• Instruction-fetch-execute cycle.
• A program to be executed by a CPU consists of set of
instructions stored in memory.
• The processor reads (fetches) instructions from memory
one at a time and executes each instruction.
• Program execution halts only if the processor is turned off,
some sort of unrecoverable error occurs, or a program
instruction that halts the processor is encountered.
5
Memory Management
• Ideally programmers want memory that is
– Large
– Fast
– Non volatile (No loss in case of power failure)
• Memory hierarchy
– Very small, extremely fast, extremely expensive, and volatile –
CPU registers
– Small, very fast, expensive, and volatile – cache
– Some medium-speed, medium price, and volatile – main
memory
– Gigabytes of slow, cheap, and non-volatile – secondary
storage
• Memory managers handle the memory hierarchy 6
7
Basic Memory Management
Three simple ways of organizing memory with OS and one user process.
(a) Used by mainframes and minicomputers (b) Used by some handheld computers
and embedded systems (c) Used by early personal computers
8
Source to Execution
• Translation of a source program in a high-level
or assembly language.
• This process generates machine language
executable code (known as a binary image)
for the source program.
• To execute binary code, it is loaded into the
main memory and the CPU state is set
appropriately.
9
Multiple Programs Without Memory Abstraction
Constant 16384 is added to every program address during the load process
Called static relocation
Illustration of the relocation problem. (a) A 16-KB program. (b) Another 16-
KB program. (c) Two programs loaded consecutively into memory
Swapping (1)
OS OS OS
Process 2 process 4
process 2 Process 4
Terminates Starts
process 3 process 3 process 3
Memory Allocation – Mechanism
• MM system maintains data about free and allocated
memory alternatives
– Bit maps - 1 bit per “allocation unit”
– Linked Lists - free list updated and coalesced when not
allocated to a process
• At swap-in or process create
– Find free memory that is large enough to hold the process
– Allocate part (or all) of memory to process and mark
remainder as free
• Compaction
– Moving things around so that holes can be consolidated
– Expensive in OS time
Memory Management - Maps
23
Placement Algorithms(1)
• 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
24
Placement Algorithms(1)
• Best Fit
– 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
• Worst Fit
– Always take the largest available hole, so that the
new hole will be big enough to be useful
25
Placement Algorithms(1)
• Quick Fit
– Maintains separate lists for some of the more common
sizes requested
– For example, it might have a table with n entries, in
which the first entry is a pointer to the head of a list of 4-
KB holes,
– The second entry is a pointer to a list of 8-KB holes, the
third entry a pointer to 12-KB holes, and so on.
– Holes of, say, 21 KB, could be put on either the 20-KB
list or on a special list of odd-sized holes.
26
Example Memory Configuration