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
Memory Management #2
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
Efficient allocation of memory enables a system to continue multitasking
Memory management is made more efficient through 3 techniques:
Paging
Segmentation
Virtual Memory
Memory Management Image
What is Paging? #1
Data stored in memory will lead to the smooth running of applications
Paging is a method of chunking the primary memory into equal-sized blocks
When an application is launched, data will be moved from the hard disk into Pages for faster access
Paging #2
As users move between applications, memory is dynamically allocated
Pages will be taken away from applications not in active use and granted to applications that are in active use
Paging can lead to internal fragmentation
If a 200KB file is divided into four 64KB Pages, the last Page would have 8KB of unused space
Unused space in a Page is wasteful because other unrelated data cannot be stored in this Page
Over time, more pockets of wasted space will exist across the memory; this process is called internal fragmentation
Paging Image
The image below shows a single 64KB Page with 4KB of unoccupied space
The box below this shows many Pages, each with varying sizes of internal fragments
What is Segmentation? #1
Segmentation is a method of chunking memory into blocks that correspond to different types of data needed by an application
A video editing application may have a Segment for video data, audio data and special effects
Segments are not all the same size; they are sized depending on their allocated data
Segmentation is space-efficient due to only allocating space depending on the amount an application needs
Segmentation #2
Segmentation can lead to external fragmentation
As Segments fill up the memory, physical gaps reduce the maximum size of new Segments that can be allocated
The arrangement of data in the segment becomes more fragmented over time because as blocks are taken away it's not possible to guarantee a new block will occupy the same amount of space
Segmentation image
Below (left) shows different application data assigned to a Segment
Below (right) shows a defragmented version of the Segment to highlight the total unused space
Virtual Memory
If a computer is running low on primary memory, it can make secondary storage act as an 'extension' of the main memory
The operating system can offload data from the primary memory into virtual memory
Virtual memory creates an illusion of a larger memory and enables applications to continue to multitask
However, accessing data in virtual memory is considerably slower compared to RAM
Solid-state drives are faster than traditional hard-disk drives, but neither are as fast as RAM
Over-reliance on virtual memory can lead to performance issues
Comparing Paging, Segmentation and Virtual Memory Image