Edexcel International Advanced Level Computer Science, point 1.2.4
Definition
Memory management is the part of the OS that controls the computer's main memory (RAM): it decides which programs get which memory, keeps their memory separate, and reuses it when programs finish.
Why it is needed
Several programs share RAM at once (1.2.2), and RAM is limited. Without memory management:
- Crashes: programs could try to use memory that is already in use, causing errors or crashes.
- Slow performance: the system could run out of memory, then slow down or freeze as it tries to handle several processes.
- Interference: one program could overwrite another's memory, corrupting its data or causing strange behaviour.
- Wasted memory: poor allocation could leave memory unused or split into pieces too small to use, so fewer programs can run.
a. Operations
| Operation | What it involves |
|---|---|
| Tracking memory allocation status | The OS keeps a record of which parts of memory are free and which are in use, and by which process |
| Determining storage requirements | Working out how much memory a program needs (code, data, stack) and whether there's enough |
| Controlling memory usage | Allocating memory when a process starts, protecting it from other processes, and freeing it when the process ends |
b. Allocation techniques
If each program has to sit in one continuous block, gaps appear as programs are closed. A new program may not fit in any one gap, even when the total free space would be enough. Moving programs around to close the gaps is slow, so the OS uses paging, segmentation or virtual memory instead.
Paging
- Idea: memory is divided into fixed-size blocks. Physical memory is split into frames, and a program's logical memory is split into pages of the same size.
- Operation:
- The OS keeps a page table for each process, which maps each page to the frame it is stored in.
- When a program is loaded, its pages go into any free frames, not necessarily next to each other.
- When a program closes, its frames are freed, and can be given to other programs.
- When the program uses a page, the OS looks it up in the page table to find the frame. If the page isn't in RAM, a page fault happens and the OS loads it from secondary storage (see virtual memory).
- Benefits: no gaps between blocks that are too small to use (no external fragmentation), easy to allocate, because any free frame will do, and each program has its own pages, so programs can't interfere with each other.
- Drawbacks: the last page of a program is usually only part full (internal fragmentation), the page table needs memory and takes time to look up, and pages ignore the structure of the program, so related code or data can be split across several pages.
Segmentation
- Idea: memory is divided into variable-size segments that match the logical parts of a program, such as code, data, stack and subroutines.
- Operation: each segment is placed in a free area of RAM that is big enough for it, so a program's segments may be in different places. A segment table holds each segment's start address and length, and maps the segment number and offset to a physical address. The offset is checked against the length to catch errors and illegal access. If a segment isn't in RAM, it is loaded from secondary storage.
| Paging | Segmentation | |
|---|---|---|
| Block size | Fixed | Variable |
| Based on | Physical memory layout | Logical structure of the program |
| Fragmentation | Internal | External |
| Table used | Page table | Segment table |
Virtual memory
- Idea: the OS uses part of secondary storage (the swap file or swap space) as if it were extra RAM, so programs can use more memory than is physically installed.
- Operation: only the pages currently needed are kept in RAM. When a program needs a page that isn't in RAM (a page fault), the OS loads it from disk, and if RAM is full it swaps out a page that hasn't been used recently.
- Benefits: larger programs can run, more programs can run at once, and each program seems to have its own large memory.
- Drawbacks: disk is far slower than RAM, so many page faults make the computer very slow. If the system spends most of its time swapping pages in and out, it is thrashing. It also needs disk space.
Stack frame
When a subroutine is called, the system creates a stack frame on the call stack (a region of memory used as a stack, controlled by the stack pointer, SP). A stack frame holds:
- The return address (where to carry on when the subroutine ends).
- The parameters passed in.
- The local variables.
- Any saved register values.
When the subroutine returns, its frame is removed (popped), and execution goes back to the return address. That's why local variables disappear after a call, and why recursion (each call has its own frame) works.
Call stack contents
A program has a main function that calls checkOrder. checkOrder first calls getPrice, then, when that has returned, calls addVAT. The call stack changes like this:
| Stage | What happens |
|---|---|
| 1 | The program starts: a frame for main is pushed |
| 2 | main calls checkOrder: a frame for checkOrder is pushed on top |
| 3 | checkOrder calls getPrice: a frame for getPrice is pushed |
| 4 | getPrice returns its result: its frame is popped, and checkOrder carries on |
| 5 | checkOrder calls addVAT: a frame for addVAT is pushed |
| 6 | addVAT returns: its frame is popped |
| 7 | checkOrder returns to main: its frame is popped |
| 8 | main finishes: the stack is empty |
Each frame holds that call's return address, parameters and local variables, so a function's local variables disappear when it returns. Too many nested calls (or infinite recursion) fill the stack and cause a stack overflow.
Worked example
Task: a computer has six page frames, numbered 0 to 5. Program P (2 pages) and then program Q (3 pages) are loaded. P is then closed, and program R (3 pages) is loaded. Show the frames after each step, and R's page table.
| Step | Frame 0 | Frame 1 | Frame 2 | Frame 3 | Frame 4 | Frame 5 |
|---|---|---|---|---|---|---|
| P and Q loaded | P | P | Q | Q | Q | free |
| P closed | free | free | Q | Q | Q | free |
| R loaded | R | R | Q | Q | Q | R |
R's page table: page 0 to frame 0, page 1 to frame 1, page 2 to frame 5.
R is stored in frames that aren't next to each other, which is fine with paging: the page table records where each page is. Without paging, R would not fit, since the largest continuous free block is only two frames.
Exam tip: when describing segmentation, say the memory is split into variable-sized segments (just saying "segments" isn't enough), and add that a segment table maps each segment to its place in physical memory. For paging, say fixed-sized pages go into any free frames, tracked by a page table. For virtual memory, mention disk used as extra RAM, page faults and the cost: it's slower. You may be asked to complete a diagram of memory usage (which frames or blocks each program uses) or of a call stack at each stage, so practise both.
Check yourself
What does the OS need to keep track of for memory management?
Which parts of memory are free and which are in use, and by which process.
What is the main difference between paging and segmentation?
Paging divides memory into fixed-size blocks regardless of program structure. Segmentation divides it into variable-size blocks that match the logical parts of a program.
What is virtual memory, and what is its main drawback?
Using secondary storage as if it were extra RAM so programs can be bigger than the installed RAM. Its drawback is that disk is much slower, so too much swapping (thrashing) makes the system slow.
What is stored in a stack frame?
The return address, the parameters, the local variables and any saved register values for a subroutine call.
Eight page frames hold A (frames 0 to 2), B (frames 3 and 4) and C (frames 5 and 6). B is closed and D needs three pages. Which frames could D use?
Frames 3, 4 and 7 (the free ones). They aren't all next to each other, but with paging that doesn't matter: D's page table records which frame each page is in.