Memory Management in Operating System
Memory Management in Operating System: What is Main Memory?
Main memory, commonly referred to as RAM (Random Access Memory), is the computer's primary temporary storage for actively processed data. Unlike permanent storage like hard drives, RAM is volatile, losing its contents when the computer powers down. It's organized into addressable cells, each holding data measured in bytes. Efficient memory management, involving allocation and deallocation, is essential for optimal performance. The amount of RAM directly impacts multitasking and program handling capabilities, making it a critical factor in overall system performance.
What is Memory Management in an Operating System?
Memory management is the operating system function that controls main memory: it tracks which parts are in use, decides which processes are loaded and where, allocates space when a process starts, and reclaims it when the process ends. It also translates logical addresses into physical ones and keeps one process out of another's memory. Memory management also keeps track of how much memory is allocated and unallocated and can move processes between memory and disk. This helps the operating system improve concurrency, system performance, and memory utilization.
Memory management handles allocating memory, keeps track of how much memory is allocated and unallocated, and can move processes between memory and disk.

Build an AI-First Career, Master the Complete Skillset
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
AI Forward Deployed Engineer Program
Full-stack engineering, production AI and client-facing consulting
+1000 moreWhy use Memory Management in OS?
-
Memory management uses various techniques to keep track of the status of each memory location, whether it is allocated or free.
-
Memory management enables computer systems to run programs that require more memory than the amount of free memory available in the computer's main memory. This is achieved by moving data between primary and secondary memory.
-
Memory management addresses the system’s primary memory by providing abstractions such that the programs running on the system perceive a large memory is allocated to them.
-
It is the job of memory management to protect the memory allocated to all the processes from being corrupted by other processes. If this is not done, the computer may exhibit unexpected/faulty behavior.
-
Memory management enables sharing of memory spaces among processes, with the help of which, multiple programs can reside at the same memory location (although only one at a time).
Logical Address Space and Physical Address Space
In the realm of memory management, distinguishing between logical address space and physical address space is paramount.
Logical Address Space encompasses the range of addresses that a CPU can generate. This is the perspective from which a program “sees” its memory. For instance, if a computer has 4 GB of RAM, the logical address space might span from 0 to 4 billion. However, it’s important to note that this space is not entirely occupied by physical memory. Instead, it serves as a convenient and abstract representation.
Physical Address Space, on the other hand, is the actual physical location in the memory hardware where data is stored. It constitutes the tangible address of a storage cell in RAM. In our previous example, if the RAM modules consist of 4 billion cells, the physical address space would correspond to each of these individual cells.
The Memory Management Unit (MMU) plays a pivotal role in this interplay. It acts as an intermediary, performing address translation by converting logical addresses to physical addresses. This enables programs to operate in a seemingly large logical address space, while efficiently utilizing the available physical memory.
Example: Consider a scenario where a program attempts to access memory address ‘x’ in its logical address space. The MMU translates this to the corresponding physical address ‘y’ and retrieves the data from the actual RAM location. This abstraction allows for efficient multitasking and memory allocation
With 4 KB pages, a logical address of 8196 gives page 2 and offset 4.
If page 2 is stored in frame 7, the physical address is 7 × 4096 + 4 = 28,676. The offset remains 4 after the translation.
You can also check out logical and physical address space to understand further.
Static and Dynamic Loading
Static Loading involves loading all the necessary program components into the main memory before the program's execution begins. This means that both the executable code and data are loaded into predetermined memory locations. This allocation is fixed and does not change during the program's execution. While it ensures direct access to all required resources, it may lead to inefficiencies in memory usage, especially if the program doesn't utilize all the loaded components.
These are the main differences between the two:
| Aspect | Static Loading | Dynamic Loading |
|---|---|---|
| When loading happens | Before execution | During execution, when needed |
| What is loaded | All required components | Components are loaded as needed |
| Memory use | Can use more memory | Avoids loading unused components |
| Main benefit | All components are available before execution | Uses memory only when a component is required |
Static and Dynamic Linking
Static Linking involves incorporating all necessary libraries and modules into the final executable at compile time. This means that the code from libraries is copied into the final executable file. The result is a self-contained executable that doesn't rely on external resources during runtime. While this ensures portability and guarantees that the program will run on any system, it can lead to larger file sizes and potential redundancy if multiple programs use the same libraries.
Dynamic Linking, on the other hand, takes a more dynamic approach. In this method, the necessary libraries are not included in the final executable. Instead, the program dynamically links to the required libraries at runtime. This means that multiple programs can share a single copy of a library, reducing redundancy and conserving memory. However, it does introduce a dependency on the availability of the required libraries at runtime.
Example: Consider a scenario where multiple programs use a common math library. With static linking, each program would contain its own copy of the library, potentially leading to larger file sizes. With dynamic linking, all programs can use the same shared instance of the library, saving disk space
Here are the key differences between them:
| Aspect | Static Linking | Dynamic Linking |
|---|---|---|
| When linking happens | At compile time | At runtime |
| Where library code is kept | Inside the executable | In a separate library |
| Executable size | Larger | Smaller |
| Main benefit | Does not depend on external libraries at runtime | Multiple programs can share the same library |
Swapping
Swapping is a technique used in an operating system for efficient management of the memory of the computer system, where an entire process may be swapped temporarily to secondary storage. Swapping includes two tasks, swapping in, and swapping out. Swapping in means placing the blocks or pages of data from the secondary memory to the primary memory. Swapping out is removing blocks/pages of data from the main memory to secondary storage. Swapping is useful when a large program has to be executed or some operation has to be performed on a large file.

Contiguous Memory Allocation
Contiguous memory allocation is a memory management technique that involves allocating a process to the entire contiguous block of the main memory it requires to execute. It is a form of partitioned allocation in which memory is assigned in contiguous areas rather than scattered locations. The simplest scheme is single contiguous allocation, where main memory is split into two partitions, with a small portion reserved for the operating system and the rest used by user processes. This means that the process is loaded into a single continuous chunk of memory. While it’s straightforward and efficient in terms of execution, it can lead to issues with fragmentation, because under contiguous allocation the system must find a large enough unbroken region for a process, and smaller blocks of memory can remain unused between allocated processes.
Memory can be allocated to processes either in one continuous block or across separate blocks. These are some of the differences that you can check out in both:
| Aspect | Contiguous Allocation | Non-Contiguous Allocation |
|---|---|---|
| Memory placement | Process is stored in one continuous block | Process can be stored in different memory blocks |
| Main techniques | Fixed and variable partitioning | Paging and segmentation |
| Fragmentation | Can cause external fragmentation | Paging avoids external fragmentation |
| Memory utilisation | May leave unused gaps between processes | Can use available memory more efficiently |
| Address translation | Simpler address calculation | Requires mapping between logical and physical addresses |
| Main limitation | A large continuous block may not be available | Requires additional memory management structures |
Memory Allocation
Memory allocation is the process of reserving a portion of the computer’s memory for a specific application or program. In partitioned allocation, memory partitions may be a fixed number in fixed sized partitions or created dynamically. It’s a crucial aspect of memory management, ensuring that each running process has enough space to execute efficiently. Effective memory allocation strategies are essential for optimizing system performance.
First Fit
First Fit is a memory allocation algorithm that allocates the first available block from free memory space that is large enough to accommodate a process. It scans the memory from the beginning and selects the first block that meets the size requirements. While it is relatively simple to implement, it can lead to fragmentation over time.
Best Fit
The Best Fit algorithm searches the entire memory space to find the smallest block that can accommodate a process. This helps in minimizing wastage of memory, as it selects the smallest available block that fits. In systems that use equal size partitions, placement is simpler, so Best Fit is more relevant when partition sizes differ. However, it may lead to more fragmented memory spaces compared to other allocation strategies.
Worst Fit
Worst Fit, as the name implies, allocates the largest available block of memory to a process. This approach can result in more fragmentation compared to First Fit or Best Fit strategies, and in partitioned schemes each partition typically holds only one process at a time. However, it can be useful in scenarios where larger processes need to be accommodated.
The three allocation strategies differ in which available memory block they choose and how much searching they require:
| Aspect | First Fit | Best Fit | Worst Fit |
|---|---|---|---|
| Picks | First hole large enough | Smallest hole that fits | Largest hole available |
| Search cost | Lowest, stops early | Highest, scans all holes | High, scans all holes |
| Leftover holes | Mixed sizes | Many tiny unusable slivers | Fewer, larger holes |
| Known for / weakness | Speed; but fragments the front of memory | Utilisation; but slivers cause external fragmentation | Reusable remainders; but wastes large holes on small jobs |
For example, consider five memory partitions of 100 KB, 500 KB, 200 KB, 300 KB and 600 KB, with four processes requiring 212 KB, 417 KB, 112 KB and 426 KB respectively. The resulting allocation is:
| Strategy | P1 (212 KB) | P2 (417 KB) | P3 (112 KB) | P4 (426 KB) | Outcome |
|---|---|---|---|---|---|
| First Fit | 500 KB block | 600 KB block | 500 KB remainder (288 KB) | Not allocated | 3 of 4 placed |
| Best Fit | 300 KB block | 500 KB block | 200 KB block | 600 KB block | All 4 placed |
| Worst Fit | 600 KB block | 500 KB block | 600 KB remainder (388 KB) | Not allocated | 3 of 4 placed |
After First Fit, the remaining holes are 100 KB, 176 KB, 200 KB, 300 KB and 183 KB, giving 959 KB of free memory in total. However, the largest available hole is only 300 KB, so the 426 KB process cannot be allocated.
After Best Fit, the remaining holes are 100 KB, 83 KB, 88 KB, 88 KB and 174 KB, giving 533 KB of free memory, and all four processes are placed.
For this particular set of partitions and processes, Best Fit places all four processes, while First Fit and Worst Fit leave the 426 KB process unallocated. This happens because each strategy chooses a different memory block for the earlier processes, leaving different amounts of free space for the last process. So, Best Fit works better for this particular set of numbers, but that does not mean it is always the best choice. Its result depends on the available memory blocks and process sizes, and it can also leave small unusable holes. For the full step-by-step allocation process, refer to contiguous memory allocation in OS.
In an exam or interview, you may be given the same set of partitions and processes and asked to work out the allocation using all three strategies. For the example above, First Fit and Worst Fit leave the 426 KB process unallocated even though 959 KB remains free, while Best Fit places all four processes. The result leads directly to the next question: why can memory remain free but still be unusable? That is the problem addressed by fragmentation, followed by paging and other memory-management techniques. Scaler's free operating system course covers these concepts together, including memory allocation and paging.
How Scaler Transformed Careers in Different Fields
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Which Memory Management Technique Should You Use?
- Contiguous allocation gives each process one continuous block of memory.
- Paging divides a process into fixed-size pages and divides memory into fixed-size frames.
- Segmentation divides a process into variable-size logical segments.
- Virtual memory uses secondary storage along with RAM when the available physical memory is not enough.
Here’s a detailed comparison for all:
| Technique | How it divides memory | Fragmentation | Hardware needed | Typical use |
|---|---|---|---|---|
| Contiguous allocation | Each process gets one continuous block | External fragmentation | Basic memory management support | Simple partitioned memory systems |
| Paging | Process is divided into fixed-size pages and memory into frames | Internal fragmentation in the last page | MMU and page table | Virtual memory and modern operating systems |
| Segmentation | Process is divided into variable-size logical segments | External fragmentation | MMU and segment table | Programs organised into logical sections |
| Virtual memory | Uses pages or segments with secondary storage to extend available memory | Depends on the underlying technique | MMU and secondary storage support | Running programs that need more memory than available RAM |
Contiguous allocation requires enough continuous memory for a process. Paging allows its pages to be placed in different frames, while segmentation keeps the program divided into logical sections. Virtual memory allows a process to use secondary storage when it needs more memory than the available RAM.
What is Fragmentation?
When processes are moved to and from the main memory, the available free space in primary memory is broken into smaller pieces. This happens when memory cannot be allocated to processes because the size of available memory is less than the amount of memory that the process requires. Such blocks of memory stay unused. This issue is called fragmentation.
Fragmentation is of the following two types:
1. External Fragmentation:
The total amount of free available primary memory is sufficient to reside a process, but the free memory space is split into non-contiguous pieces. External fragmentation can be decreased by compaction or shuffling of data in memory to arrange all free memory blocks together and thus form one larger free memory block, though some memory blocks remains unused because the scattered gaps cannot satisfy a request.
Turn Learning into Career Growth
2. Internal Fragmentation:
Internal fragmentation occurs when the memory block assigned to the process is larger than the amount o memory required by the process. In such a situation a part of memory is left unutilized because it will not be used by any other process. Internal fragmentation can be decreased by assigning the smallest partition of free memory that is large enough for allocating to a process.

What is Paging?
A computer system can address and utilize more memory than the size of the memory present in the computer hardware. This extra memory is referred to as virtual memory. Virtual memory is a part of secondary memory that the computer system uses as primary memory. Paging has a vital role in the implementation of virtual memory.
The process address space is a set of logical addresses that every process refers to in its code. Paging is a paging technique of memory management in which the process address space is broken into fixed size units. These paging pages are of the same size and are referred to as “pages”. The size of a page is a power of 2 and its value is in the range of 512 bytes to 8192 bytes. The size of a process is measured in terms of the number of pages.
A similar division of the main memory is done into blocks of fixed size. These blocks are known as page frames in the computer's primary memory, and the size of a frame is the same as that of a page to achieve optimum usage of the primary memory and to avoid external fragmentation.
Paged memory management uses a data structure called a page table to map pages to frames on a page basis.
Following are some Advantages of Paging:
-
Paging decreases external fragmentation.
-
Paging is easy to implement.
-
Paging adds to memory efficiency.
-
Since the size of the frames is the same as that of pages, and frames are fixed size blocks, swapping becomes quite simple.
-
Replacement algorithms can improve performance when deciding which page should leave memory.
-
Paging is useful for fast accessing data.

What is Segmentation?
Segmentation in OS is a memory management technique in which a process is divided into variable-size segments. Each segment represents a logical part of the program, such as the code, data, stack, or functions. Unlike paging, where all pages have the same fixed size, segments can have different sizes depending on the amount of memory required by each part.
Each segment has a segment number and a starting address. The operating system uses a segment table to keep track of these segments and their locations in memory. When a process refers to a logical address, the memory management unit uses the segment number to find the corresponding segment and then uses the offset to locate the required data.
Since segments have different sizes, they can be allocated in different areas of physical memory. However, this can result in external fragmentation when free memory is split into small blocks.
What is Virtual Memory?
Virtual memory in OS is a memory management technique that allows a process to use more memory than is physically available in RAM. The operating system uses a part of secondary storage as an extension of main memory and moves data between RAM and secondary storage as required.
A process does not need to have all its data and instructions in RAM at the same time. The parts currently needed can be kept in main memory, while other parts can remain in secondary storage. This allows larger programs to run even when the available physical memory is limited. The Linux kernel's memory management documentation covers the memory-management mechanisms used by the kernel.
Paging is commonly used to implement virtual memory. The process is divided into pages, and the operating system loads the required pages into available page frames. When a required page is not in RAM, it can be brought in from secondary storage.
Virtual memory allows better use of available memory, but frequent movement of data between RAM and secondary storage can slow down the system. When this movement becomes excessive, it can lead to thrashing, which can significantly reduce system performance.
FAQs
Q. What is Fragmentation and How Does it Affect Memory Allocation?
A. Fragmentation occurs when memory is divided into small, non-contiguous blocks. It can lead to inefficiencies in memory usage, and contiguous allocation is a common source of external fragmentation. The article briefly mentioned fragmentation, but it might be beneficial to explain it in more detail.
Q. Can a Program Access Memory Outside its Allocated Space?
A. It’s important to clarify how memory protection mechanisms prevent programs from accessing memory locations that haven’t been allocated to them. This ensures that programs don’t interfere with each other.
Q. How Does Virtual Memory Enhance Memory Management?
A. Virtual memory is a crucial concept that allows programs to use more memory than is physically available. Memory management techniques include contiguous allocation, paging, and segmentation memory management. This can be a significant factor in modern memory management systems.
Q. What is Thrashing in Memory Management?
A. Thrashing occurs when a computer’s performance severely degrades due to excessive swapping of data between RAM and secondary storage. This can be caused by heavy movement between RAM and secondary storage when physical memory is insufficient for the tasks being performed.
Q. What is Segmented Memory?
A. Segmented memory divides a user's program into several segments of variable length, and segmentation memory management tracks them in a segment table. Each segment has segment numbers, a starting address, and related control information in a segment map table. Because segments do not form one contiguous address space, hardware support is often required.
Conclusion
-
Memory management in OS is a technique of managing the functionality of primary memory, used for achieving better concurrency, system performance, and memory utilization.
-
Memory management keeps track of the status of each memory location, whether it is allocated or free.
-
Paging is a technique of memory management in which the process address space is broken into blocks. All the blocks are of the same size and are referred to as “pages”.
-
Fragmentation is of two kinds, external and internal.
-
Segmentation is the method of dividing the primary memory into multiple blocks. Each block is called a segment and has a specific length.
-
wapping includes two tasks, swapping in, and swapping out.