Memory Management: Virtual Memory
The core conception in memory management is of address space mapping.
Difference between physical memory and virtual one is: the virtual must be continuous but physical can be fragmental.
Dreamy Memory
The OS tries to make an illusion of using memory of the following properties(desired): private, infinitely large, infinitely fast, nonvolatile, cheap memory
.
Memory Hierarchy
However, in real world, the memory follows the hierarchy:
Faster access from left to right, higher price from left to right | ||||
---|---|---|---|---|
Registers(1 cycle) | Cache(~10 cycles) | Main Memory(~100 cycles) | Flash Disk(~1M cycles) | Traditional Disk(~10M cycles) |
Primary Storage | ||||
On CPU | ||||
Not cared by OS | cared by OS(OS memory management) |
Cycle here can be interpreted as nano-second(a speed measurement)
Goals of OS Memory Management
Every access to memory is in virtual instead of physical one, due to the virtual mapping by OS.
- Main memory allocation
- location of kernel in address space
- what memory to allocate processes
- how many processes to allow
- Protection(Segeregation)
- no corruption to OS and other processes
- Privacy: can’t read data from other processes
- Transperancy
- Illusion of private memory for each process
Protection
Protection to be leaded to prevent trespassing access to memory space.
The general idea is to add a check in the course of accession.
The check is a hardware device (MMU Memory Management Unit).
Transparency
Essence is to make sure the running of process won’t cause confliction.(no worry of actual running space)
Main Memory Allocation
Where to allocate the kernel?
In the low memory address. The reason is the interrupt vectors(the address mapping of hardware device of transferring between user and kernel mode) are in the low memory.
Physical and Virtual Memory
The early days OS runs only single process at a time which is trusted so no virtual(logical) memory is needed; the process directly access the physical memory.
However, if the process is malicious(tresspass into the kernel) or there are multiple processes runs at the sam time, the model won’t work. Thus, logical(virtual) memory is needed.
- Virtual/logical address space = what the program thinks is its memory
- Physical address space = actually is its memory(residing physically)
CPU(process) –(generates virtual address space)–> MAP(MMU) –(Transfer)–> Memory(Physical Address)
Mapping Scheme
Classification
- Base and Bounds
- Segmentation
- Paging
For each scheme, we need to understand: physical address space, virtual address space, virtual address, MMU.
Base and Bounds
- Virtual Address Space
Linear address space: from 0 to MAX - Physical Address Space
Linear address space: from BASE to BOUNDS = BASE + MAX
MMU - MMU
MMU check relocation register(holds the base value) and limit register(holds the bounds value). - Free Space Distribution
OS will allocate the new process to the holes(space which is free).
Free list : A list of the range of the physical memory not in use.- Method
- Take first hole bigger than requested
- The smallest hole bigger than requested
- The largest hole -> tries to eliminate many unusable smallest holes
- Method
The problem of base and bounds is there may be huge “free space” occupied by the process in the memory that can’t be reallocated (internal fragmentation problem).
One solution is to implement by segmentation.
Segmentation
In stead of treat one program as a chunk in address space, we segmented it in three parts: Code, Heap, Stack which can grow dynamically.
The segmentation has many ways, the above 3 parts is only one of them.
Can be treated as multiple base and bounds.
Virtual Address Space
Two-dimensional space with set of segements and each segment ‘i’ is linear from 0 to MAX(i)Set of segments, each linear
The order of the segments in physical space has no need to be the same as in virtual address space.
Virtual Address (Structure)
Check Lecture
Segment + Offset
–> Number of segment bits + offset bits = virtual memory address bitsMMU
- Segment table
Indexed by segment number
contains (base,limit pair)- base: physical address of segment in memory(where the segment starts)
- limit: length of the segment
- Pointer to segment table in memory
- length of segment table
notice a recursive use in MMU
- Segment table
Advantage and Disadvantage
- Disadvantage
Segmentation requires the segmentation table(a data structure) to be stored in MMU, which is convoluted. - Advantage
The segmentation table actually enables memory sharing between processes.
Sharing is not possible with base&bounds but possible with segmentation.
Memory Sharing
Memory sharing in segementation can be easily reached by having the same point address in the segmentation table.
Protection Bits
To guarantee the protection property of memory allocation of OS in the course of memory sharing, protection bits by extra hardware support are needed.
Protection bits is a few more bits per segment indicating the permission of read, write,and execute.
Compaction
Compaction is a way to solve fragmentation problems(holes) in the physical memory. It can be used in both base&bounds and segmentation.
However, compaction is inefficient:
- Process needs to be stopped for rearranging
- data need to be copied to somewhere
- MMU table changed
Besides, if a segment is needed to be expanded after compaction, the compaction needs to be restarted again.
NOTICE:
The info of mapping is all stored in MMU.
Internal fragmentation only appears in base&bounds since in segmentation the virtual spaces are all occupied. But external fragmentation happens in both.Internal Fragmentation: can take free space inside the virtual memory space
External Fragmentation: there’s holes(free space) in the physical memory