All subjects · Computer Science · WCP01 Principles of Computer Science
Memory management: paging, segmentation, virtual memory and the call stack
Edexcel IAL Computer Science · WCP01 Principles of Computer Science
The OS must track which memory is free, decide how much each process needs and stop processes interfering. Spec point 1.2.4 covers how it allocates memory and how a call stack stores stack frames.
What memory management does
- Tracks allocation status: records which areas of memory are free and which are allocated, and to which process.
- Determines storage requirements: works out how much memory a process needs and finds space for it.
- Controls memory usage: allocates and frees memory, protects each process's area from others, and moves data between RAM and secondary storage when RAM is full.
Paging and segmentation
- Paging: memory is divided into equal-sized physical blocks called frames, and each process into blocks of the same size called pages. Pages can go into any free frames, so a process need not be in one continuous block. A page table maps each page to its frame. Simple to manage; but the last page of a process is often partly empty (internal fragmentation).
- Segmentation: a process is divided into segments of different sizes that match its logical parts, such as code, data and stack. A segment table records each segment's start and length. Segments match the program's structure and can be shared or protected separately; but variable sizes leave gaps of unusable free memory between segments (external fragmentation).
Virtual memory
Virtual memory uses part of secondary storage as if it were extra RAM. When RAM is full, pages not currently needed are swapped out to disk; when they are needed again, they are swapped back in.
- Benefit: more or larger programs can run than physical RAM would allow.
- Drawback: secondary storage is far slower than RAM. If pages are swapped in and out constantly, the computer spends more time swapping than processing (disk thrashing). Adding RAM reduces this.
The call stack and stack frames
When a subroutine (function) is called, a stack frame is pushed onto the call stack. It holds:
- the return address (where to continue after the call),
- the parameters passed in,
- the subroutine's local variables.
The stack pointer holds the address of the top of the stack. When the subroutine returns, its frame is popped and execution continues at the return address. Nested or recursive calls add a frame each time; too many causes a stack overflow.
Worked example
main() calls area(4, 5), and area calls square(4). Show the call stack while square is running.
- Bottom: frame for main.
- Next: frame for area, holding the return address in main, parameters 4 and 5, and its local variables.
- Top: frame for square, holding the return address in area, parameter 4 and its local variables.
- The stack pointer points to the square frame.
Answer: Top to bottom: square, area, main. Frames are popped in reverse order as each function returns.
Check yourself
- A process of 13 KiB is loaded into a system that uses 4 KiB pages. How many pages are needed?
Show answer
4. 13 / 4 = 3.25, so 4 pages (pages cannot be split).
- In the same system (4 KiB pages, 13 KiB process), how many KiB of the last page are unused?
Show answer
3 KiB. 4 pages = 16 KiB; 16 - 13 = 3 KiB of internal fragmentation.
- Which statement describes segmentation?
- Memory is divided into equal-sized frames
- Secondary storage is used as extra RAM
- The CPU checks for interrupts after every instruction
- A process is divided into variable-sized blocks that match its logical parts
Show answer
A process is divided into variable-sized blocks that match its logical parts. Segments are logical units such as code, data and stack.
- What is disk thrashing?
- Spending more time swapping pages between RAM and disk than executing processes
- A hard disk spinning too fast
- Deleting files to free up space
- Pages being stored in cache
Show answer
Spending more time swapping pages between RAM and disk than executing processes. Too little RAM for the active pages leads to constant swapping.
- Which item is not stored in a stack frame?
- The return address
- The parameters passed to the subroutine
- The page table for the whole system
- Local variables of the subroutine
Show answer
The page table for the whole system. The page table belongs to the memory manager, not to a function call.
- Explain one benefit and one drawback of virtual memory.
Show answer
Benefit: more programs, or larger programs, can run than the physical RAM allows. Drawback: secondary storage is much slower than RAM, so swapping slows the computer and can lead to thrashing.. One benefit (capacity) and one drawback (speed, thrashing).
- When a function returns, what happens to its stack frame?
- It stays on the stack until the program ends
- It is copied to secondary storage
- It is popped and execution continues at the return address
- It is moved to the bottom of the stack
Show answer
It is popped and execution continues at the return address. The stack is last in, first out; the finished function's frame is removed.
- Which type of fragmentation is associated with paging?
- External fragmentation
- Data fragmentation on a network
- Packet fragmentation
- Internal fragmentation
Show answer
Internal fragmentation. Fixed-size pages leave unused space inside the last page.
Sources
Last updated 8 November 2026. Spotted a mistake? Tell us.