OCR H446, section 1.2.1 (b)
Many programs are in memory at once, and each needs its own space. Memory management is the job of the operating system that allocates memory to programs, keeps them apart, and makes the best use of the available RAM.
What memory management must do
- Allocate memory to a program when it starts, and free it when it ends.
- Protect each program's memory, so one can't read or overwrite another's.
- Let programs be larger than the free memory that is available.
- Avoid wasted space, which is called fragmentation.
If programs were simply loaded one after another in RAM, gaps would be left as they close, and eventually there would be plenty of free memory in total but no single gap big enough for a new program.
Paging
In paging, memory is divided into blocks of equal size.
- Physical memory (RAM) is split into frames.
- A program's logical memory is split into pages, of the same size as a frame (such as 4 KB).
- A page table records which frame holds each page. The pages of a program don't need to be next to each other in RAM.
Translating an address
A program uses a logical address, made of a page number and an offset (the position within the page). The OS uses the page table to find which frame the page is in, giving the physical address.
| Paging | |
|---|---|
| Block size | Fixed |
| Unit seen by the programmer | Not visible: pages are just a division of memory |
| Fragmentation | No external fragmentation. Some space is wasted inside the last page (internal fragmentation) |
Segmentation
In segmentation, a program is divided into segments of different sizes, according to its logical parts: for example the code, the data, and the stack.
- Each segment is a meaningful unit, and its size matches what it holds.
- A segment table records the start address (base) and length (limit) of each segment.
- An address is a segment number and an offset. The OS checks that the offset is within the limit, which gives protection.
| Paging | Segmentation | |
|---|---|---|
| Block size | Fixed | Variable |
| Divided by | The system (no meaning) | The program's logical structure |
| Fragmentation | Internal | External (gaps of various sizes appear between segments) |
| Good for | Simple, efficient use of RAM | Protection and sharing of whole parts such as code |
Some systems combine the two (segmented paging).
Virtual memory
Virtual memory lets a computer run programs that need more memory than the RAM installed. The OS uses part of the hard disk or SSD (the swap file or page file) as an extension of RAM.
- Only the pages in use are kept in RAM. The rest stay on the disk.
- When a program needs a page that isn't in RAM, a page fault happens. The OS loads the page from the disk into a free frame. If RAM is full, it first moves another page out to the disk (swapping), often one that hasn't been used recently.
- The program is unaware of this. It appears to have a large, continuous memory.
Advantages
- Programs can be bigger than RAM.
- More programs can run at once.
- Each program has its own protected address space.
Disadvantages
- The disk is much slower than RAM, so a page fault causes a delay.
- If RAM is too small for the programs running, the system spends most of its time swapping pages in and out. This is called thrashing, and makes the computer very slow.
Worked example
Task: a computer has 4 GB of RAM, but the user runs programs needing 6 GB in total. Explain what happens.
- The OS keeps the pages currently needed in RAM, and puts the less used pages in the page file on the disk (virtual memory).
- When the user switches to a program whose pages are on the disk, a page fault occurs, and the OS loads them into RAM, swapping out other pages to make space.
- The programs all work, but switching can be slow, because the disk is slower than RAM. If it happens constantly, the computer is thrashing. Adding more RAM would fix this.
Exam tip: for paging, say fixed-size pages and frames, with a page table. For segmentation, say variable-size logical segments. For virtual memory, mention the disk used as an extension of RAM, page faults, and the slowdown (thrashing) when RAM is short.
Check yourself
What is the difference between paging and segmentation?
Paging divides memory into fixed-size pages and frames. Segmentation divides a program into variable-size segments that match its logical parts, such as code and data.
What is virtual memory?
Using part of the secondary storage as an extension of RAM, so that programs can use more memory than is physically installed.
What is a page fault?
When a program needs a page that is not currently in RAM, so the operating system has to load it from the disk.
Why does too much swapping make a computer slow?
The disk is much slower than RAM, so if pages are constantly moved in and out (thrashing), the processor spends most of its time waiting.