Virtual Memory
- Virtual memory is a technique that allows the execution of processes which are not completely available in memory.
- The main visible advantage of this scheme is that programs can be larger than physical memory.
- Virtual memory is the separation of user logical memory from physical memory.
- This separation allows an extremely large virtual memory to be provided for programmers when only a smaller physical memory is available.
- Following are the situations, when entire program is not required to be loaded fully in main memory.
- User written error handling routines are used only when an error occurred in the data or computation.
- Certain options and features of a program may be used rarely.
- Many tables are assigned a fixed amount of address space even though only a small amount of the table is actually used.
- The ability to execute a program that is only partially in memory would counter many benefits.
- Less number of I/O would be needed to load or swap each user program into memory.
- A program would no longer be constrained by the amount of physical memory that is available.
- Each user program could take less physical memory, more programs could be run the same time, with a corresponding increase in CPU utilization and throughput.
- Virtual memory is commonly implemented by demand paging. It can also be implemented in a segmentation system.
- Demand segmentation can also be used to provide virtual memory.
Paging
- External fragmentation is avoided by using paging technique.
- Paging is a technique in which physical memory is broken into blocks of the same size called pages (size is power of 2, between 512 bytes and 8192 bytes).
- When a process is to be executed, its corresponding pages are loaded into any available memory frames.
- Logical address space of a process can be non-contiguous and a process is allocated physical memory whenever the free memory frame is available.
- Operating system keeps track of all free frames. Operating system needs n free frames to run a program of size n pages.
Address generated by CPU is divided into
- Page number (p) -- page number is used as an index into a page table which contains base address of each page in physical memory.
- Page offset (d) -- page offset is combined with base address to define the physical memory address.
Segmentation
- Segmentation is a technique to break memory into logical pieces where each piece represents a group of related information.
- For example, data segments or code segment for each process, data segment for operating system and so on.
- Segmentation can be implemented using or without using paging.
- Unlike paging, segment are having varying sizes and thus eliminates internal fragmentation.
- External fragmentation still exists but to lesser extent.
Address generated by CPU is divided into
- Segment number (s) -- segment number is used as an index into a segment table which contains base address of each segment in physical memory and a limit of segment.
- Segment offset (o) -- segment offset is first checked against limit and then is combined with base address to define the physical memory address.