Skip to content

JA5. Memory Fragmentation

Statement

  • Describe the issue of memory fragmentation and the use of free space in a system.
  • Analyze and explain what might be the best and worst-case scenario for an operating system with respect to memory fragmentation.

Answer

Introduction and definitions

  • logical address: it is a virtual memory address referring to the location of a variable (pointer) in the process’s memory, sometimes referred to as a relative address.
  • physical address: it is the actual address of the variable in relation to the entire main memory, sometimes referred to as an absolute address.
  • logical address space is the set of all logical addresses allocated to a process.
  • physical address space is the set of all physical addresses allocated to a process.
  • At runtime, the OS’s Memory Management Unit (MMU) translates logical addresses to physical addresses; by adding the base address (the physical address of the first byte of the process’s memory) to the logical address.
  • Processes never see physical addresses, only logical addresses.

Memory management

  • The OS manages the memory of the system, allocating and de-allocating memory to processes.
  • When a process is created, the OS allocates a chunk of memory to it, and keeps track of the boundaries (physical addresses) of that chunk of memory.
  • After creating, the process is loaded into memory, and the OS sets the base address of the process’s memory to the physical address of the first byte of the process’s memory.
  • The process loads its code to the first part of its memory, then adds its execution stack, and initializes a heap space; all of these three are local to the process’s address space.
  • Stack and heap are growing in nature, stack holds the local variables of the process and its function calls, and heap holds the dynamic memory allocations of the process.
  • When a process terminates, the OS de-allocates the memory chunk allocated to it; and reclaim it back to be used by other processes.
  • When a process long runs, it may uses all of its space, causing a stack overflow (when the heap meats the stack and process runs out of memory), and crashing.
  • In these conditions, OS (and MMU) may transfer some parts of the process’s memory to a secondary memory (disk or another memory device, or even to the cloud), so that it frees up some space for the process to continue running. MMU will keep track of the boundaries of the process in the main memory and secondary memory.
  • When a process needs to access a value, MMU will fetch it either from the main memory or from the secondary memory, and then return it to the process.
  • There are multiple techniques to manage memory transition to the second storage, such as paging, segmentation, and Swapping.
  • Swapping: a mechanism where an entire process is swapped out from main memory tp secondary memory (storage disk in this case), allowing more space for other processes to run; and then swapped back to main memory when needed.
  • Paging: a mechanism where the process’s memory is divided into fixed-size blocks called pages (pages in secondary memory, and frames in the main memory). Idle pages are swapped out to secondary memory, and swapped back when needed. The addresses of pages and frames are stored in a page table.
  • Segmentation: a mechanism where the process’s memory is divided into variable-size blocks called segments. Process segments may scatter through out memory, so the address space for such process is not contiguous. Idle segments are swapped out to secondary memory, and swapped back when needed. The addresses of segments and segments are stored in a segment map table.

Memory fragmentation

  • Memory fragmentation: is a problem that occurs when the OS allocates memory to processes, and the memory is not contiguous.
  • As long as processes are loaded into memory, memory is deviled into contiguous chunks of memory, and each chunk is allocated to a process.
  • When a process terminates, the OS de-allocates its memory, and breaking it down into smaller chunks, then re-allocating them to other processes.
  • Since processes require a contiguous chunk of memory, it may happen that the OS cannot find a contiguous chunk of memory to fit the process, although the is enough memory in the system.
  • There are two types of memory fragmentation:
    • External fragmentation: is the fragmentation that occurs when the OS cannot find a contiguous chunk of memory to fit a process, although there is enough memory in the system. Can be solved by using compaction.
    • Internal fragmentation: is the fragmentation that occurs when the OS allocates a chunk of memory to a process, but the process uses only a part of the chunk, leaving the rest of the chunk unused. can be solved by using paging.

Best case scenario in memory fragmentation

  • The best case scenario regarding memory fragmentation is when:
    • The address space of each process starts exactly at the end of the previous process’s address space, where the first process starts at the first byte of the memory.
    • Each process knows exactly how much memory it needs, and the OS allocates exactly that amount of memory to it.
    • All processes starts and ends at the same time, and the OS de-allocates all processes at the same time, so no gaps are left in the memory.
    • In this case, the memory is fully utilized, and there is no external fragmentation, nor internal fragmentation.
  • The best case scenario is almost impossible to achieve practically, but it is the best case scenario theoretically.

Worst case scenario in memory fragmentation

  • The worst case scenario regarding memory fragmentation is when:
    • processes are loaded into memory in close timing of each other.
    • each process uses a different amount of memory, that is not known in advance (may need more memory in the middle of execution). internal fragmentation is high.
    • each process ends at a different time (randomly).
    • processes then start to come at a fixed rate, with their execution time and memory usage being random.
    • After a while, the memory is fragmented, and the OS cannot find a contiguous chunk of memory to fit a process, although there is enough memory in the system (external fragmentation is high).

References