Memory Management In Operating Systems

Thormiwa
4 min readJun 20, 2023

--

Memory management is a crucial aspect of operating systems that deals with the allocation, utilization, and organization of computer memory. It basically makes sure that programs and data have the necessary memory resources to perform efficiently.

Memory management is responsible for keeping track of which parts of memory are allocated to which processes, managing memory allocation and deallocation, and protecting processes from interfering with each other.

Lets break it down into concepts:

  1. Memory Hierarchy: The memory hierarchy includes registers, cache, main memory (RAM), and secondary storage devices like hard drives. The operating system manages the movement of data between these levels to optimize performance.
    We can think of memory hierarchy like shelves in a library. There are multiple shelves in a library each with different access speeds and capacities. Registers are like the librarian’s table where books that are frequently accessed for are kept. Cache is like a small shelf beside the librarian’s desk where books that are currently in use are held. RAM is like the main bookshelve holding a collection of books and secondary storage devices like hard drives e.t.c is like a the library basement where less frequently accessed books are held.

For example if you wanted a book from the library, the librarian searches for it in this method
REGISTER → CACHE → RAM(MAIN MEMORY)-> SECONDARY STORAGE
The goal is to basically find the book in the fastest possible location (e.g., registers or cache) before resorting to slower memory levels like main memory or the hard drive.

2. Address Spaces: Each process in an operating system has its own address space, which is a range of memory addresses that it can access. The address space is divided into smaller units called pages or segments, depending on the memory management scheme used.
Let’s imagine a big whiteboard where each processes can read and write information. Every process has their own section on the whiteboard called an address space, represented by a specific range of address it can address.

For example if you have two processes running on your machine. Process P and Process Q. Process P has an address space ranging from 1 to 100, Process Q has an address space ranging from 101 to 200. This separation makes sure that each process runs independently without interfering with each other.

3.Memory Allocation: When a process is created or when it requests memory during its execution, the operating system needs to allocate memory to that process. There are different memory allocation strategies, such as fixed partitioning, dynamic partitioning, or virtual memory.

Memory allocation is like assigning seats in a classroom. When students enter the classroom, the teacher needs to allocate seats to them based on availability.

Let’s take an example…A classroom with 20 seats (representing available memory). When a new student (process) arrives, the teacher (operating system) assigns them an empty seat if available. If all seats are occupied, the teacher may need to rearrange students or ask some to leave (memory deallocation) to accommodate the new student.

4.Memory Protection: Memory protection makes sure that a process cannot access or modify the memory assigned to another process without proper authorisation . The operating system uses hardware features like memory protection units and access control mechanisms to enforce memory protection.

Memory protection is like locking individual lockers at a gym. Each gym member (process) has their own locker (memory space), and they should not be able to access someone else’s locker without proper authorisation. The gym management ensures that only the rightful locker owner (process) can access their locker’s contents.

5. Paging: Remember we talked about address space earlier….Paging is a memory management scheme that divides the address space of a process into fixed-size pages and the physical memory into fixed-size page frames. The operating system maps pages to page frames to enable efficient memory allocation and retrieval. Paging helps in reducing external fragmentation and enables virtual memory.

Let’s Imagine a big puzzle (process) with 100 pieces (pages). However, you only have a small table (physical memory) that can fit 20 puzzle pieces (page frames) at a time. So, you work on 20 pieces at a time, placing them on the table. If you need to work on another set of pieces, you’ll have to swap them with the pieces you’re not currently using.

I hope these simplified examples will help you understand Memory Management a lot better as it did for me.

Thanks for Reading :)

--

--