What is Memory Management in OS?
Memory management in OS is the process of regulating and organizing computer memory in order to allocate and deallocate memory space efficiently for programs and applications that require it. This helps to guarantee that the system runs efficiently and has enough memory to run apps and tasks.
Functions of Memory Management in OS
Some of the major functions fulfilled by memory management are discussed below.
Memory Allocation
Memory management ensures that the needed memory space is allocated to the new process whenever a process is created and requires memory. Memory Management also keeps track of the system's allocated and free memory.
Memory Deallocation
Like memory allocation, whenever a process completes its execution, memory management ensures that the space and the memory resources it holds are released. Any newly created process can use the freed memory.
Memory Sharing
Memory sharing is also one of the main goals of memory management in OS. Some processes might require the same memory simultaneously. Memory management ensures that this is made possible without breaking any authorization rules.
Memory Protection
Memory Protection refers to preventing any unauthorized memory access to any process. Memory management ensures memory protection by assigning correct permissions to each process.
Why is Memory Management Required in OS?
- Memory management allows computer systems to run programs that need more main memory than the amount of free main memory available on the system. By moving data between primary and secondary memory, memory management can be achieved
- Memory management maintains track of the status of every memory location, whether it is allocated or free.
- It is the responsibility of memory management to protect the memory allocated to all the processes from being corrupted by other processes. The computer may exhibit faulty/unexpected behavior if this is not done.
- Memory management allows sharing of memory spaces between processes, with the help of which multiple programs can consist at the same memory location.
- Memory management takes care of the system's primary memory by giving abstraction such that the programs running on the system perceive a large memory is assigned to them.
Role of Memory Management in OS
Memory management in an operating system (OS) is crucial in efficiently utilizing the computer's memory resources. It's responsible for allocating and deallocating memory to different processes and ensuring they don't interfere with each other. Memory management keeps track of which parts of memory are in use and which are available, preventing conflicts and crashes. It enables processes to share memory and optimizes memory usage to avoid wastage. Proper memory management ensures that applications run smoothly, preventing crashes due to memory exhaustion and enabling seamless multitasking, making the computer more reliable and efficient.
Techniques Memory Management in OS
Let's see different memory allocation schemes:
Contiguous Memory Management Schemes
Contiguous memory management schemes are a type of memory management method in which processes are assigned fixed-size memory blocks. When a process requests memory, it is assigned to the first available large enough block, and the available memory space is partitioned into equal-sized blocks. Contiguous memory management systems can be static or dynamic, depending on whether memory allocation occurs at compile time or run time. These memory management strategies are widely employed in operating systems and are crucial.
Non-Contiguous Memory Management Schemes
By allocating memory blocks of varied sizes to processes, non-contiguous memory management schemes allow more flexibility than contiguous memory management systems. This method allows memory to be allocated in non-contiguous sections, allowing for more efficient use of memory space. Paging and segmentation are two commonly used non-contiguous memory management mechanisms in current operating systems.
Memory Allocation Schemes
Swapping
Swapping is a mechanism for temporarily moving a process from main memory to secondary storage and making that memory available to other processes. The system switches the process from secondary storage to main memory at a later time.
For example, Consider a multiprogramming environment with a round-robin CPU scheduling. When a quantum of a process expires, the memory manager will begin swapping out and swapping another process into the free memory space.
Note: Though the swapping process degrades performance, it helps in concurrent execution of various large processes, which is why it’s also known as a Memory Compaction method.
Paging
Paging is a memory management technique that allows a process’s physical address space to be noncontiguous. External fragmentation and the necessity for compaction are avoided by paging. It also solves the significant difficulty of fitting memory chunks of various sizes onto the backing store, which plagued most memory management techniques before the introduction of paging.
Most operating systems use paging in one form or another. Traditionally, hardware has been responsible for paging support. Recent designs, particularly on 64-bit microprocessors, have accomplished paging by tightly integrating the hardware and operating system.
Basic Methods to Implement Paging
Breaking physical memory into fixed-sized blocks called frames and logical memory into blocks of the same size called pages is the most basic way for implementing paging.
When a process is ready to run, its pages are loaded from their source into any available memory frames. The backing store is organised into fixed-size blocks that are the same size as the memory frames.
Advantages of Paging
Reduced External Fragmentation: Paging reduces external fragmentation since memory allocation is based on fixed-size pages rather than variable-sized partitions.
Simplicity: Paging is considered a straightforward memory management approach, making it easier to implement and understand compared to other methods.
Ease of Swapping: Swapping processes in and out of main memory is simplified because pages and frames are of identical size, facilitating efficient memory management.
Disadvantages of Paging
Internal Fragmentation: While external fragmentation is reduced, paging introduces internal fragmentation due to allocating entire pages even if only a portion of the page is needed.
Increased Memory Overhead: Page tables, which map virtual addresses to physical addresses, consume additional memory space proportional to the size of the address space, which can be substantial and not suitable for memory-constrained systems.
Segmentation
The separation of the user’s view of memory from the actual physical memory is a crucial feature of memory management. Segmentation is a memory management strategy that supports the user view of memory.
Now, you might be thinking, what is the user view?
A user view means what a user thinks of a program. A user sees a program as the main method, variables, Data Structures, library functions etc. A user doesn’t think about their addresses in memory.
In segmentation, a job is divided into several smaller segments. Each module contains parts that execute related functions. Each segment corresponds to a different program’s logical address space. Each of these segments has a different length, determined by the segment’s function in the program. Segments are given a segment number for ease of implementation.
As a result, a logical address is made up of two tuples: <segment-number, offset>.
Advantages of Segmentation
Flexibility in Memory Allocation: Segmentation allows variable-sized memory allocation segments, accommodating data structures and processes of varying sizes more efficiently than fixed-size partitions.
Protection and Sharing: Segments can be assigned different access permissions (read-only, read-write, execute-only), enhancing security by restricting access to critical segments and facilitating data sharing among processes.
Simplification of Addressing: Segmentation simplifies memory addressing by using a two-dimensional address space (segment number, offset within segment), making it easier to manage large memory spaces and supporting modular programming and dynamic loading
Disadvantages of Segmentation
- Possibility of external fragmentation.
- Allocating contiguous memory to variable-sized partitions is difficult.
- Segmentation is a costly memory management technique.
Fragmentation
The above memory allocation strategies suffer from fragmentation problems. Once the processes are loaded and removed from memory, free memory space is divided into smaller pieces. Fragmentation, in general, means the total memory space is sufficient to meet the request, but the available space is not continuous.
For example, consider a multi-partition allocation scheme with an 18,464-byte block of free memory. Suppose a process requests for 18,462 bytes. If we allocate the requested block in the available block, we will be left with a 2-byte hole. The cost of tracking this hole will be much higher than the hole itself.
Memory fragmentation is of two types: internal or external. Let’s see them one by one.
Internal Fragmentation
The memory block allotted to the process is larger than the needed space. Because another process cannot use it, some memory is left unused in that block. This is known as internal fragmentation. It can be minimised by designating the smallest partition that is large enough for the process.
External Fragmentation
When total available memory space is sufficient to satisfy a process request, it is not contiguous. It cannot be used for memory allocation. This type of fragmentation is known as external fragmentation. Compaction or shuffle memory contents to place all free memory in one large block can reduce external fragmentation
What is Memory Allocation?
Memory Allocation is assigning memory segments to processes based on their requirements. Memory allocation uses many different algorithms to determine the segments to be selected for the process allocation. The three most common algorithms include First Fit, Best Fit, and Worst Fit.
First Fit
In this algorithm, the operating system searches for the first free memory block in the memory that is big enough to fit the current memory requirement. The memory block is then allocated to the process, and the memory table is updated. The first fit is a comparatively easy algorithm to implement, but it results in external fragmentation, which is not suitable.
Best Fit
The Best fit algorithm searches for the small available memory segment that is big enough to fit the current memory requirement. This method is used to reduce the problem of external fragmentation by using the smallest available block. However, since we need to scan the whole memory table, it can result in slower allocation.
Worst Fit
The Worst Fit algorithm works in contrast to the Best Fit algorithm. It searches for the largest available block and allocates it to the process. This method intentionally maximizes external fragmentation so other smaller processes can be allocated the leftover partition.
Frequently Asked Questions
What are the methods of memory management?
Although different operating systems use other methods for memory management, all of these methods aim at efficiently allocating and deallocating memory resources. Some of the most common memory management methods include Paging, Segmentation, Fragmentation, Swapping, etc.
What is memory management in OS in embedded system?
Memory management in operating systems for embedded systems involves efficient allocation, utilization, and control of memory resources. It focuses on optimizing memory usage to meet the specific constraints and requirements of embedded devices, ensuring reliability, performance, and real-time responsiveness.
What is the difference between process management and memory management?
Process management involves handling processes, including process creation, scheduling, synchronization, and termination, ensuring efficient CPU utilization. Memory management, on the other hand, deals with allocating, protecting, and freeing memory for processes, optimizing memory usage and preventing conflicts.
What are the five requirements of memory management?
The five requirements of memory management involve allocation, deallocation, protection, relocation, and sharing. Allocation refers to assigning memory to a process. Deallocation is memory release. Protection involves preventing unauthorized memory access. Relocation refers to the movement of processes in memory. Sharing enables multiple processes to access the same memory.
Conclusion
This article extensively contains a detailed description of Memory Management in Operating Systems (OS), various ways of organizing memory, and various memory -management techniques, including paging and segmentation etc.
Recommended Readings:
For a much deeper understanding of operating system concepts, we recommend exploring the operating system guided path provided by Code360. You can also consider our Operating System Course to give your career an edge over others.
Do upvote our blog to help other ninjas grow. Happy Coding!