Memory Management in Operating System
Subject: Computer Science Fundamentals · Language: Operating System · Level: intermediate · 10 min read
Learn memory management with logical and physical addresses, allocation methods, fragmentation, paging, segmentation, protection, and swapping.
Memory management is the operating system function that controls main memory, also called RAM. Every running program needs memory to store instructions, variables, data, stack, heap, and temporary results. Since RAM is limited, the OS must manage it carefully. When many programs run at the same time, they all need memory. The OS decides which process gets memory, how much memory it gets, where it is placed, and when memory should be released. Good memory management improves speed, safety, and system stability.
For beginners, memory management can be understood as room allocation in a hostel. Many students need rooms, but rooms are limited. The manager must assign rooms, track empty rooms, prevent two students from using the same bed, and free rooms when students leave. The OS does similar work with memory.
Why Memory Management Is Needed
Memory management is needed because RAM is limited and expensive compared to storage. A computer may run many processes, but all of them cannot use unlimited memory. The OS must keep track of used and free memory. It must allocate memory to new processes and release memory after processes finish. It must also protect one process from accessing another process's memory. Without memory management, one faulty program could overwrite another program's data or even damage operating system data. This would make the system unstable and insecure.
Main Memory and Secondary Storage
Main memory, or RAM, stores programs and data that are currently active. The CPU can access RAM quickly. However, RAM is volatile, which means data is lost when power is turned off. Secondary storage, such as hard disk or SSD, stores data permanently. It is slower than RAM but can store much more data. Programs are stored on secondary storage and loaded into RAM when they run. Memory management mainly deals with RAM, but it also works with secondary storage when virtual memory is used.
Address Space
An address space is the range of memory addresses that a process can use. Each process usually has its own logical address space. This gives the process the feeling that it has its own memory area. The OS and hardware translate logical addresses used by a process into physical addresses in RAM. This translation helps protect processes and allows flexible memory placement.
Logical Address and Physical Address
A logical address is generated by the CPU while a program is running. It is also called a virtual address in many systems. The program works with logical addresses, not directly with real RAM addresses. A physical address is the actual location in main memory. The Memory Management Unit, or MMU, converts logical addresses into physical addresses. This separation is important because a process does not need to know where it is actually placed in RAM. The OS can move or manage memory more flexibly.
Address Binding
Address binding means mapping program instructions and data to memory addresses. It can happen at compile time, load time, or execution time. Compile-time binding happens if the memory location is known before execution. Load-time binding happens when the program is loaded into memory. Execution-time binding happens while the program is running and is common in modern systems. Execution-time binding provides flexibility because a process can be moved in memory during execution with the help of hardware support.
Contiguous Memory Allocation
In contiguous memory allocation, each process is placed in one continuous block of memory. For example, if a process needs 100 KB, it receives one continuous 100 KB memory area. This method is simple to understand and manage. However, it can create fragmentation problems. Over time, free memory may become split into small pieces. Contiguous allocation was used in older systems and is useful for understanding basic memory allocation ideas.
Fixed Partitioning
In fixed partitioning, memory is divided into fixed-size partitions. Each partition can hold one process. The partition sizes are decided before execution. Fixed partitioning is simple, but it may waste memory. If a process is smaller than its partition, the unused part inside that partition is wasted. This is internal fragmentation. Also, if a process is larger than any available partition, it cannot be loaded even if total free memory is enough across multiple partitions.
Variable Partitioning
In variable partitioning, partitions are created dynamically based on process size. If a process needs 120 KB, the OS tries to allocate a 120 KB block. This reduces internal fragmentation, but it can create external fragmentation. Free memory may be available, but it may be split into small non-continuous blocks. Variable partitioning needs allocation strategies such as first fit, best fit, and worst fit.
First Fit, Best Fit, and Worst Fit
First fit allocates the first free block that is large enough for the process. It is fast because the OS does not search the entire memory every time. Best fit allocates the smallest free block that is large enough. It tries to reduce wasted space, but it may create many tiny unusable gaps.
Worst fit allocates the largest available block. The idea is to leave a reasonably large remaining block after allocation, but it may not always perform well. These strategies are used to decide where a process should be placed in memory when variable partitioning is used.
Fragmentation
Fragmentation means memory is wasted or divided in a way that reduces efficient use. There are two main types: internal fragmentation and external fragmentation. Internal fragmentation happens when allocated memory is larger than required memory. The unused memory inside the allocated block is wasted. External fragmentation happens when free memory exists but is split into small pieces. A process may need a large continuous block, but no single free block is large enough even though total free memory is sufficient.
Compaction
Compaction is a technique used to reduce external fragmentation. The OS moves processes in memory so that free spaces combine into one large block. Compaction can help create large free blocks, but it takes time and requires support for moving processes during execution. Because of this, it is not always practical.
Paging
Paging is a memory management technique that divides physical memory into fixed-size blocks called frames and divides a process into fixed-size blocks called pages. A page can be placed in any available frame. Pages of the same process do not need to be stored continuously in memory. This removes external fragmentation.
The OS uses a page table to map page numbers to frame numbers. When a process generates a logical address, the system uses the page table to find the actual physical address. Paging may still have internal fragmentation because the last page of a process may not be completely filled. However, external fragmentation is avoided.
Segmentation
Segmentation divides a program into logical parts called segments. Examples of segments include code segment, data segment, stack segment, and heap segment. Each segment represents a meaningful part of the program. Unlike paging, segments can have different sizes. This matches the programmer's view of a program more naturally. Segmentation can suffer from external fragmentation because segments are variable-sized. However, it provides logical separation and protection for different parts of a program.
Paging vs Segmentation
Paging divides memory into fixed-size pages and frames. It is mainly designed for efficient memory allocation and avoiding external fragmentation. Segmentation divides memory based on logical program parts. It is easier to understand from a programmer's point of view but can cause external fragmentation. Many modern systems use paging heavily, and some use ideas from both paging and segmentation.
Memory Protection
Memory protection prevents one process from accessing another process's memory without permission. This is important for safety and security. If a process tries to access memory outside its allowed area, the OS can stop it and show an error. This prevents one faulty program from corrupting the entire system. Memory protection is achieved using hardware and OS support such as base-limit registers, page tables, access bits, and privilege levels.
Swapping
Swapping is a technique where a process is temporarily moved from main memory to disk and brought back later. This helps when memory is full and the OS needs space for other processes. The area on disk used for this purpose is often called swap space. Swapping allows the system to run more processes than can fit in RAM at one time, but too much swapping can slow the system because disk is much slower than RAM.
Summary
Memory management controls RAM allocation, address translation, protection, and efficient use of memory. It is needed because many processes share limited memory. The important beginner concepts are logical address, physical address, address binding, contiguous allocation, fixed partitioning, variable partitioning, first fit, best fit, worst fit, internal fragmentation, external fragmentation, compaction, paging, segmentation, memory protection, and swapping.
More Operating System Lessons
- Introduction to Operating System
- Types of Operating System
- Operating System Functions and Services
- System Calls in Operating System
- Process Management in Operating System
- Process Scheduling in Operating System
- CPU Scheduling Algorithms
- Threads and Multithreading in Operating System
- Process Synchronization in Operating System
- Deadlock in Operating System
- Virtual Memory in Operating System
- File System in Operating System