What is memory management?
-
Memory management is a fundamental role of the operating system, dealing with the allocation and deallocation of the computer’s primary memory
-
When a user opens an application, its data is loaded from storage into active memory so that it can run smoothly
-
When a user opens a file from the file system, e.g. word document, the CPU loads this file data, as well as application data, into the primary memory
-
Primary memory is a limited resource in the system, so it needs careful management
-
Benefits of memory management are:
- Efficient allocation of memory enables multitasking, allowing multiple programs to run at once
- Memory management maintains security, it does not let programs access memory reserved for other programs
-
Memory management is made more efficient through 3 techniques:
- Paging
- Segmentation
- Virtual Memory
Links between different types of memory
Examiner Tips and Tricks
- The main benefit of memory management is to make computer systems run smoothly. Most users don’t realise that as they effortlessly move between applications, it’s made possible because the OS is rapidly reallocating memory depending on the task that the user is completing.
- Make sure you can name one benefit and one drawback for each memory management method in this revision note.
Paging
What is paging?
- Paging is a memory management technique that divides physical memory into fixed-sized blocks called pages.
- When an application is launched, its instructions and data are moved from secondary storage into these pages. Because pages are a uniform size, they can be stored in non-contiguous (scattered) locations in RAM, allowing for more efficient use of available memory.
- Paging is a physical division of memory. This means the OS splits a programme into pages based on size alone, without any consideration for its logical structure (e.g., a single loop or function might be split across two different pages).
- Memory is dynamically managed; pages are allocated to applications in active use and can be swapped out to virtual memory if they are no longer immediately required.
- A significant disadvantage of paging is internal fragmentation. Since pages are a fixed size, a file or process rarely fits perfectly into the final allocated page.
- For example, if a 200KB file is divided into 64KB pages, it will require four pages:
- Page 1: 64KB used (Full)
- Page 2: 64KB used (Full)
- Page 3: 64KB used (Full)
- Page 4: 8KB used, 56KB unused
- The 56KB of unused space in the final page is “wasteful” because the OS cannot store unrelated data from a different process inside that same page.
- This creates small “pockets” of wasted space across the RAM, which reduces the overall efficiency of memory utilisation.
- Arbitrarily splitting programmes can also impact performance; for instance, if a frequently used loop is split across two pages, the processor may have to jump between memory locations more often, potentially increasing the number of page faults.
Internal fragmentation
Segmentation
What is segmentation?
- Segmentation is a memory management technique that divides memory into variable-sized blocks called segments, which correspond to the logical units of a programme.
- Unlike paging, which uses fixed physical divisions, segments represent logical structures such as functions, modules, or arrays. For example, a video editing application may have separate segments for video data, audio data, and special effects.
- Segments are not a uniform size; they are sized dynamically according to the specific requirements of the data or instructions they contain.
- This approach preserves the logical structure of a programme, which can improve performance and make it easier for the operating system to debug or protect specific sections of code.
- A significant disadvantage is that memory allocation is more complex because the OS must find contiguous blocks of memory to fit variable-sized segments.
- Segmentation can lead to external fragmentation. As segments are loaded and removed, physical gaps appear between them in the memory.
- These gaps reduce the maximum size of new segments that can be allocated; over time, the memory may have enough total free space for a programme, but not in a single contiguous block.
- The image below (left) shows different application data assigned to a segment, illustrating how they are grouped by their logical relationship.
- The arrangement of data in memory becomes more fragmented over time because when a segment is removed, it is not possible to guarantee that a new segment will be the exact same size as the space left behind.
- The image below (right) shows a defragmented version of the memory to highlight the total amount of unused space wasted by external fragmentation.
External fragmentation
Virtual Memory
What is virtual memory?
- Virtual Memory is a memory management technique that utilises a portion of secondary storage (disk) to extend the available capacity of the main memory (RAM)
- Virtual Memory enables the execution of programs even when their total size exceeds the capacity of the available physical memory
- The operating system offloads data by moving inactive pages or segments from RAM to the disk and retrieves them when needed
- Virtual Memory successfully creates the illusion of a much larger, contiguous memory space for applications, thereby enabling effective multitasking
- Accessing data stored in Virtual Memory is considerably slower than accessing data in physical RAM, even with faster Solid-State Drives (SSDs)
- Over-reliance on constant swapping leads to thrashing, a severe performance issue where the system spends more time managing transfers than executing instructions
| Memory Management Technique | Description | Example | Benefits | Drawbacks |
|---|---|---|---|---|
| Paging | Divides memory into fixed-sized blocks (pages) | A process needing 200KB of memory is divided into four 64KB pages, leaving 8KB unused in the last page | Facilitates efficient memory management and enables the use of virtual memory | This can lead to internal fragmentation |
| Segmentation | Divides memory into variable-sized segments based on logical parts of a process | In a video editing application, different segments may be created for video data, audio data, effects, and UI elements | Allows for intuitive and efficient memory access | This can result in external fragmentation |
| Virtual Memory | Uses hard drive space as an ‘extension’ of RAM | When memory-intensive applications exceed the available RAM, the OS moves less frequently accessed pages to the hard disk | Allows more extensive programs to be run and facilitates multitasking | Slower to access than physical memory, which degrades performance if overused |
Examiner Tips and Tricks
- Virtual memory is a method of memory management.
- Not to be confused with Virtual Storage, which is storing files in the cloud.
Worked Example
Describe how the operating system would use virtual memory to load program C when there’s not enough space in physical memory.
[3]
How to answer this question:
- The operating system can use virtual memory to act as an extension of the computer’s primary memory. This means less-critical data can be offloaded from the primary memory into virtual storage, useful when a higher-priority set of processes require immediate attention. If the OS offloaded data from the RAM into virtual memory, this would free up space for program C to be loaded into RAM.
Answer:
Answer that gets full marks: The contents of the accumulator (9 in this case) will be copied to the MDR [1 mark] and then 9 is copied to location 16. [1 mark]
Acceptable answers you could have given instead:
The value 16 is copied to the MAR. [1 mark]
Worked Example
Imogen buys a desktop computer. It comes with an operating system installed.
Describe two ways that an operating system could manage physical memory.
[4]
How to answer this question
- Recall two methods of memory management (Paging, segmentation, virtual memory)
- Explain how each of these works and how they assist in memory management
Answer:
Example answer that gets full marks: Imogen’s operating system could manage physical memory in two ways. Paging, which is a method of dividing memory into fixed-size chunks known as pages. Application data can be allocated and deallocated to pages making it a flexible system depending on the activity of the user.
The system could also use virtual memory, a method of extending the available physical memory by using a portion of the hard drive. This allows more programs to run simultaneously and enhances the overall system performance.
Acceptable answers you could have given instead:
Imogen’s computer uses paging to manage memory. This means it breaks down the memory into fixed-size pieces and swaps them in and out as needed.
The computer also uses something called virtual memory. It uses part of the hard drive to act like extra memory, so more programs can run at the same time, and everything works faster.