Memory Management
Memory Management
Introduction
A program resided on a disk as a binary executable file (.exe). to be executed the program must
be loaded into memory and placed within a process for it to be run.
Compile-Time: this is absolute code any changes to code the whole program must be
recompiled .
Load-Time : happen when loading module or system library .compiler must generate
relocatable code.
Execution Time : if the process can be moved during its execution from one memory
segment to another , example creating object of class or dynamically load modules or system
library . And its need hardware support such as (base and limit registers ) .
Compile Time
Object module 2
Linker RunTime library
Object module 3
Load-Time
Dynamic loaded
Memory
system library
Swapping
A process is swapped temporary out of memory to secondary storage like HDD , and then
brought back into memory to continue execution .
Swapping requires :
Fast secondary storage
Large secondary storage
Must provide direct access
Swapping is used for : priority -based scheduling algorithms : where lower-priority process is
swapped out and higher-priority is swapped in Or the memory is full .
Note : Transfer time it takes more time than swapping .
Contiguous memory allocation
Operating system
Memory
No
Error
Mutliple-partition
Each process has its own partition .
Hole : block of available memory , and they vary in size are scattered throughout memory .
The hole must be greater or equal the process size .
OS maintains information about:
Allocated partitions
Free partitions (hole)
Example :
OS
OS 400
P1
P4
P3
P1 400
1000
1000
P2
2000 1700
P3 2300
2560 2560
Worst-fit : allocate the largest hole , must also search the entire list .
Worst-fit = descending order + First-fit .
Note : fist-fit is faster
First-fit + best-fit better storage utilization
Fragmentation
When processes is swapped out of memory , the pieces free memory are scattered into large
number of small holes the problems are called
External fragmentation
Total memory space exists to satisfy a request , but it is not contiguous .
Solution :External fragmentation is solved by Compaction , paging , Segmenation
Compaction : is possible only if relocation is dynamic and done in execute time. And its very
expensive .
Algorithm : move processes location to make all the smalls holes contiguous .
Paging: make address space of a process non-contagious , thus process is allocated physical
memory whenever it is available .
Frames : divide the physical memory into fixed-size blocks
Pages : divide logical memory into blocks of same size called page .
FrameSize = page size
OS maintains :
Keep track of all free frames
To run program of size n pages , need to find n free frames
Set up a page table to translate logical to physical address
Page offset
1 1
2 2 Frame 2
Page 1
Algorithm :
1) convert page size and memory size to 2 power n
2) power of memory size – power of page size to divide address to page number and
offset .
3) convert logical to binary
4) page number section points to the frame using page table
5) physical address = frame * framSize + offset
example
page size = 4-bytes Page number frame number
Advantages of paging
There is no external fragmentation
Internal fragmentation is minimized (on average ½ frame waste )
Small frames reduce internal fragmentation , but increase the size of page table
Case #2
One Page table register (PTBR) points to the page table for that process , PTBR changed by the
OS whenever a process is dispatched , or context-switching
Note : every access now requires 2 memory access
1) One access to get the page table entry
2) One more to get the data/instruction stored
Memory protection
Associating protection bit with each frame in page table
Valid -bit : indicate that the page is in the process logical address space . and is indicated by
“V”
Invalid-bit : indicate that the page is not in the process logical address space . and its
indicated by “I”.
Multilevel paging
Was used to solve the problem when the page table size is very large and cant be stored in
main memory . by paging the page table .
Page offset
Shard pages
Pages can be shared among processes, not all pages can be shared they must be
“ Reentrant code “ only read
ED1 3
3
Data1 4
4
Process A Process A page table
Main memory
ED1 3
Data2 4
Process B page table
Process B
Process A
Segment table
Internal fragmentation
Allocated memory may be slightly larger than requested memory , so the process takes the
entire hole .
Virtual – memory
Technique that allows to execute process that may not be completed in memory
Demand paging
How it works? Bring a page into memory only when it is needed
Advantages :
Less I/O needed
Less memory needed
Faster response
More users
Difference between paging and demand paging ?
Demand Paging : is similar to paging but there is swapping .
Note : when brining page from secondary storage to memory , and there is no free frame the
OS make swap in -out .
Page fault when brining page from secondary storage to memory . and set the invalid -valid
bit
Page fault steps (handling) :
1) If there is ever a reference to page , the OS will bring the Page from HDD to Memory
2) OS look at the page table to decide :
If invalid reference abort
Or its is not in memory
3) Get empty free frame
4) If memory is not full , swap page into memory
5) Reset table , vaild bit = 1
6) Execute
What happen in page fault when there is no free frame ?
Page replacement algorithm . algorithm must result in minimum number of page faults