Memory Management

GCSE Computer Science Resources
14-16 Years Old

48 modules covering every Computer Science topic needed for GCSE level, and each module contains:

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module
View the GCSE Resources →

KS3 Computing Resources
11-14 Years Old

We’ve created 45 modules covering every Computer Science topic needed for KS3 level, and each module contains:

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module
View the KS3 Resources →

What is Memory Management?

Memory management is an activity, which is carried out in the kernel of the operating system. The kernel itself is the central part of an operating system, it manages the operations of the computer and its hardware, however it’s most known for managing the memory and the CPU time.

One of the key functions of the memory management system in a computer is assigning memory to a number of different running programs to keep the performance of the system stable.

Memory in the system is dynamically allocated depending on the requirement, and it is freed up when the process no longer requires the memory, thus alloxcating that slot of memory to another process if needed.

Ultimately memory management will depend on the how effective the configuration is in the hardware, operating system, and programs or applications.

Hardware: At hardware level, memory management involves physical devices that store the data. An example of this would Random Access Memory (RAM), furthermore this also includes memory caches and flash based SSD’s (Solid State Drives).

Operating System: At the operating system level, memory management involves the allocation of specific memory blocks to individual programs as user demand changes.

For example, if the user switches from a word document to the Internet. 

Applications: At application level, memory management will make sure that each application objects and data structures get their required portion of memory at its required pace.

Modern computer systems manage memory at two levels: application level and system level.

(Application level is categorised as being either automatic or manual memory management)

With an address space, memory management is either categorised as being automatic memory management i.e. (which involves garbage collection) or manual memory management.

Garbage collection: The collector attempts to take back “garbage”, which means to reclaim memory that is no longer occupied by the objects that are no longer used by the program.

Automatic memory management:

Automatic memory management is a mechanism, in which an operating system or application automatically manages the allocation and deallocation of memory.

Therefore, unlike manual memory management, the programmer does not have to write code to manage the tasks associated with memory management when developing an application.

Automatic memory management eliminates problems such as forgetting to free memory allocated to an object, which may be causing memory leaks.

Manual memory management:

Manual memory management involves the usage of manual instructions set by the programmer, these instructions will identify and deallocate used objects, or garbage from the memory.

One of the main problems associated with memory management is: Memory leaks.

Memory leaks are a failure in the program to release discarded memory, which will cause either a decrease in performance and ultimately failure.

Memory Management Allocation techniques

(Useful definitions)

External fragmentation:

External fragmentation occurs when there’s a sufficient quantity of area within the memory to satisfy the memory request of a method, though the process’s memory request cannot be fulfilled because the memory offered is during a non-contiguous manner.

Internal fragmentation:

Internal fragmentation happens when the memory is split into mounted sized blocks.

The mounted sized blocks are allotted to the method whenever a method requests for memory.

Contiguous Memory Allocation:

Contiguous Memory Allocation is an allocation model that assigns a process consecutive memory blocks (memory blocks having consecutive addresses)

Main memory is a hardware resource, which has physical addresses. These addresses are used to locate areas in which data and instructions can be stored.

When a program is executed, a series of logical addresses are produced. The sum of those logical addresses will make up the logical address space of that process.

Swapping:

One of the key aspects of memory management is swapping. It is when a process is swapped temporarily from the main memory to the secondary storage (like a disk), thus making that memory available for other processes. Then at some later time, the system will swap back the process from the secondary storage to the main memory. This helps the performance of multiple big processes in parallel.

Logical address:

A logical address is an address, which is generated by the CPU when the program it’s relevant to is running. Logical addresses are also known as “virtual addresses”, as they do not exist physically.

These addresses are used as a reference to access the physical memory location by the CPU.

The memory management unit, which is the hardware device, is used for mapping logical addresses to its corresponding physical address.

Furthermore the operating system has to map the logical address space to the physical address space and manage memory usage between the processes as appropriate, for instance via segmentation, paging, or the use of virtual memory.

Paging

When a logical address is split, it is divided into memory units which referred to as pages, furthermore, when this page is loaded into main memory, it is stored in a page frame, which is a block of sequential addresses that are the same size (meaning they have the same number of addresses) as the page.

The functionality of paging allows memory to be allocated in a non-contiguous manner, that means that pages of the same process do not need to be stored together, though it can be allocated wherever there is free space in the main memory.

Page table: A page table is the data structure used by a virtual memory system in a computer operating system to store the mapping between visual address and physical addresses.

Page replacement techniques

Demand paging:

Demand paging is a type of swapping that is done in virtual memory systems. Demand paging as it says from the title, only copies data from the disk to the RAM if the data is required by some program, therefore meaning that the data will not be when the data is already available on the memory. This can be referred to as “lazy evaluation”, because only the demanded pages of memory are being swapped from the secondary storage (disk space) to the main memory.

Anticipatory paging:

Also referred to as “swap prefetch”, this is when the operating system attempts to anticipate data that will be needed next and copies it to the RAM before it is actually required, this technique reduces the chances of future page faults.

Page fault: This is when a type of exception occurs that is raised by the computer hardware when a running program accesses a memory page that is not currently mapped by the memory management unit.

Free page queue, stealing, and reclamation:

This is a list of page frames that are available for assignment, this technique prevents the queue from being empty, which therefore minimises the computing necessary to service a page fault.

Page stealing refers to operating systems that continuously look for pages that have not been recently referenced, they free the page frame and then add it to the free page queue.

Moreover, some operating systems also support page reclamation, which is when a program commits a page fault by reference a page that was “stolen”, the operating system will then detect this and reclaiming the page frame. This is done without having to read the contents back to into the RAM.

Pre-cleaning:

Pre-cleaning is when an operating system continuously pre-cleans “dirty” pages.

Dirty page: A dirty page in an operating system refers to pages in memory (page cache) that has been rationalised and therefore it has changed for what is currently stored on the disk. Dirty pages usually occur when an existing file on the disk is appended or altered.

Pre-cleaning involves writing the modified pages back to the disk, despite them being further modified. This technique will minimise the amount of cleaning that is needed to obtain a new page frame, which is needed at the moment a new program initiates or a new data file is opened. Pre-cleaning also improves responsiveness.

Advantages of paging includes:

  • Paging reduces external fragmentation.
  • Simple to implement.
  • Swapping becomes easy to implement as pages and frames are of the same size.

Disadvantages of paging includes:

  • Suffers from internal fragmentation
  • Page tables require extra memory space, so if a system has small RAM, it won’t function as efficient.

Segmentation:

To fit the varying memory requirements of each process, memory blocks, which are allocated to processes that are divided into segments of different sizes. The segments can be moved in and out of the memory as required, meaning they don’t have to be stored continuously across a fixed address space.

In order for the system operating system to track the allocation of memory for each process, it uses a segment table, which records where each single segment required for a specific process is physically located.

Segmentation works very similarly to paging, although with segmentation, the segments are of variable length the as in paging they are of fixed size.

Segmentation without paging:

This requires the entire segments to be swapped back and forth between main memory and the secondary storage. Therefore when a segment is swapped the operating system will allocate enough contiguous free memory to hold the entire segment.

Segmentation with paging:

This involves individual pages moving back and forth between main memory and secondary storage. Pages can be allocated anywhere in the main memory and therefore is not contiguous.

Advantages of segmentation includes:

  • No internal fragmentation.
  • Segment table consumes less space in comparison with a page table.

Disadvantages of segmentation includes:

  • When processes are loaded and removed from the memory, the free space, which they occupied, is broken into little pieces, casing external fragmentation.

Virtual memory

Other than the system storage, the Random Access Memory(RAM), the operating system can extend the limited physical space of memory by using other storage in the computer, such as the use of the hard disk, then when required again, swap the process back into main memory when it is needed, though accessing a secondary storage medium such as the hard drive is considerably slower.

Virtual memory is a peripheral in the operating system, which enables a computer to be able to compensate shortages of physical memory (RAM) by transferring pages of data from Random Access Memory (RAM) to disk storage to deny overloading, furthermore virtual memory is designed to work temporary in combination with RAM.

Therefore, when RAM runs close to full capacity, virtual memory can move data from the RAM to a space which is referred to as a “paging file”. This allows the RAM on the system to free up space so that the computer can continue with the main execution/task.

Virtual memory, also regarded to be logical memory, is a memory management technique performed by the operating system. The main use of virtual memory is, it allows the users to use more memory for applications/programs/tasks than the available physical memory (RAM) on the system.

One of the main advantages of virtual memory is it ensures memory protection by converting the memory address to the corresponding physical address. It does this by extending the use of physical memory by using the hard disk, though it prevents accessing the main memory directly.

Advantages of Virtual Memory

  • Allows more than one program to be executed at the same time.
  • Virtual memory increases the overall memory on a system without adding RAM, this is advantageous as virtual memory is less expensive.
  • Eliminates external fragmentation.
  • Less input/output is required, which leads to faster and easy swapping of processes.

Disadvantages of Virtual Memory

  • If the system relies to much on virtual memory, it may cause a decrease in performance.
  • The more virtual memory that’s being used, the less disk space a user has for storage.
  • Time it takes to read from a magnetic disk is greater than the time to access RAM, therefore swapping should be avoided wherever performance is important. Ultimately meaning that it would take greater time to switch between applications.

Page replacement policies

(Linked list: In computer science a linked list refers to a linear data structure where each element is a separate object, though the elements in a linked list are not stored in at a contiguous location, these elements are lined using pointers.)

When a new page requires to be brought into memory for the specific process/applications/task, it will be often necessary to remove one that is currently already there. Some of the algorithms, which take of this are listed below.

Least Recently Used Algorithm (LRU):

The LRU algorithm replaces whichever page has remained unreferenced for the greatest amount of time.

Least recently used page replacement is an algorithm which works on the theory that pages, which had been most heavily used in the past few instructions are most likely to be used heavily in the next few instructions too. While LRU could potentially provide near optimal performance, they are expensive to implement in practice, moreover there are few implementation methods for this algorithm that try to reduce the cost but yet have the same performance.

Not Recently Used (NRU):

This algorithm works like LRU, although it does not have as much overhead. Each page frame has a page referenced bit correlated to it, and that reference is set to 1 only if the page is referenced (as the operating will reset all the pages to 0 (periodically) ), therefore any page referenced bit of 0 will be eligible for replacement.

In summary, this algorithm likes to keep pages that only have been recently used.

First in First Out (FIFO):

FIFO replaces the page, which has been in memory for the longest time, though it based that the page is unlikely to be in use.

A linked list of pages, which is chronologically ordered is used to decide which page has been in memory the longest amount of time and is unlikely to be used.

The page at the top of the list is removed, and the new page is added to the back of the list.

FIFO may result in poor efficiency, though because some of the pages that were removed may be in frequent use, there removal from memory will result in additional page faults being generated.

Clock:

The clock algorithm can be recognised to be a variation of the FIFO algorithm, though the difference is that a “circular” linked list is used and that each entry in the list has a page referenced bit, which is initially set to 0, and it is set to 1 each time the page will be referenced. The pointer of the linked list moves around the list until a page frame with a page referenced bit of 0 is found (if all the page references are 1, the pointer will return to its starting point).

Summary and Facts

What is Memory Management?

Memory management is an activity, which is carried out in the kernel of the operating system.

Memory management can be defined to be the process of controlling and coordinating computer memory, assigning portions that are referred to as blocks, to various running programs to optimise the overall system performance.

The operating system, programs, applications, and hardware all have memory management systems.

Memory Management Allocation techniques:

Main memory is a hardware resource, which has physical addresses. These addresses are used to locate areas in which data and instructions can be stored.

When a program is executed, a series of logical addresses are produced. The sum of those logical addresses will make up the logical address space of that process.

Furthermore the operating system has to map the logical address space to the physical address space and manage memory usage between the processes as appropriate, for instance via segmentation, paging, or the use of virtual memory.

Paging:

When a logical address is split, it is divided into memory units which referred to as pages, furthermore, when this page is loaded into main memory, it is stored in a page frame, which is a block of sequential addresses that are the same size (meaning they have the same number of addresses) as the page.

Page replacement techniques:

  • Demand paging
  • Anticipatory paging
  • Free page queue, stealing, and reclamation
  • Pre-cleaning

Segmentation:

To fit the varying memory requirements of each process, memory blocks, which are allocated to processes that are divided into segments of different sizes. The segments can be moved in and out of the memory as required, meaning they don’t have to be stored continuously across a fixed address space.

Virtual memory:

Other than the system storage, the Random Access Memory(RAM), the operating system can extend the limited physical space of memory by using other storage in the computer, such as the use of the hard disk, then when required again, swap the process back into main memory when it is needed, though accessing a secondary storage medium such as the hard drive is considerably slower.

Key differences between Paging and Segmentation:

CharacteristicPagingSegmentation
Memory sizeIn paging, a process address is broken into fixed sized blocks called pagesIn segmentation, an address is space is broken into a varying sized blocks called sections
AccountabilityOperating system divides the memory into pagesThe compiler is responsible to calculate the segment size, the virtual address and actual address
SizePage size is ultimately determined by the available memorySection size is determined by the user
SpeedPaging is faster in terms of memory accessSegmentation as a whole is slower than paging
FragmentationMay cause internal fragmentation as some pages may go underutilsiedMay cause external fragmentation as some of the memory block may not be used at all
Logical addressLogical address is divided into page number and page offsetLogical address is divided into section number and section offset
Data storagePage table stores the page dataSegmentation table stores the segmented data

Page replacement policies:

  • Least Recently Used Algorithm (LRU)
  • Not Recently Used (NRU)
  • First in First Out (FIFO)
  • Clock

References:

  1. https://www.interserver.net/tips/kb/virtual-memory-demand-paging/
  2. https://isaaccomputerscience.org/concepts/sys_os_memory_management
  3. https://en.wikipedia.org/wiki/Manual_memory_management
  4. https://en.wikipedia.org/wiki/Memory_segmentation
  5. https://en.wikipedia.org/wiki/Page_fault
  6. https://www.tutorialspoint.com/operating_system/os_memory_management.htm
  7. https://www.techopedia.com/definition/3769/contiguous-memory-allocation
  8. https://www.interviewbit.com/courses/programming/topics/linked-lists/#:~:text=A%20linked%20list%20is%20a,has%20a%20reference%20to%20null.
  9. https://brainly.in/question/3197766#:~:text=Dirty%20pages%20are%20the%20pages,disk%20is%20altered%20or%20appended.
  10. technologyuk.net/computing/computer-software/operating-systems/memory-management.shtml
  11. https://www.techopedia.com/definition/27271/automatic-memory-management-amm#:~:text=Automatic%20memory%20management%20(AMM)%20is,tasks%20when%20developing%20an%20application.
  12. https://simple.wikipedia.org/wiki/Kernel_(computer_science)#:~:text=A%20kernel%20is%20the%20central,which%20contains%20many%20device%20drivers.

Leave a Comment