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 in OS is an important function of the operating system that manages primary memory. It is used for achieving better 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.

Why 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
Transform Your Career
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
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.
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
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 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.
Turn Learning into Career Growth
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.
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.

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.