Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Memory Management in OS?
3.
Functions of Memory Management in OS
4.
Why use Memory Management in OS?
5.
Role of Memory Management in OS
6.
Memory Allocation Schemes
6.1.
Contiguous Memory Management Schemes
6.2.
Non-Contiguous Memory Management Schemes
7.
Memory Management in OS Techniques
7.1.
Swapping
7.2.
Paging
7.2.1.
Basic Methods to Implement Paging
7.2.2.
Advantages and Disadvantages of Paging
7.3.
Segmentation
7.3.1.
Disadvantages of Segmentation
7.4.
Fragmentation
7.4.1.
Internal Fragmentation
7.4.2.
External Fragmentation
8.
What is Memory Allocation?
8.1.
First Fit
8.2.
Best Fit
8.3.
Worst Fit
9.
Frequently Asked Questions
9.1.
What is memory management in Operating System?
9.2.
What are the methods of memory management?
9.3.
What are the five requirements of memory management?
10.
Conclusion
Last Updated: Mar 27, 2024
Easy

Memory Management in Operating System (OS)

gp-icon
Operating system track
Free guided path
14 chapters
83+ problems
gp-badge
Earn badges and level up

Introduction

With the introduction of modern operating systems, the performance of a CPU attained new heights. Several processes are handled by the CPU at one time, increasing the overall performance of the system. 

memory management in os


Memory is an essential component of a computer used to store data. Because the amount of main memory available in a computer system is fairly restricted, its management is crucial to the computer system. The primary memory management function of an operating system is the most critical. It facilitates the movement of processes between the main memory and the execution disc. 

In this article, we will discuss memory management in OS (Operating System)in detail. 
So let’s get started.

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 use Memory Management 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.

Memory Allocation Schemes

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 Management in OS Techniques

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 and Disadvantages of Paging

The following is a list of the advantages and disadvantages of paging.

  • External fragmentation is reduced by paging, but internal fragmentation remains.
     
  • Paging is a simple memory management approach that is believed to be effective.
     
  • Swapping is simple due to the identical size of the pages and frames.
     
  • Page tables demand additional memory space. Hence they may not be suitable for systems with limited RAM.

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>.

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 is memory management in Operating System?

Memory management refers to the effort of dividing memory across many tasks. Memory management is an operating system approach for managing actions between main memory and disc during process execution.

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 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 Coding Ninjas Studio. 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!

Previous article
What is Loader?
Next article
Input-Output systems
Guided path
Free
gridgp-icon
Operating system track
14 chapters
83+ Problems
gp-badge
Earn badges and level up
Live masterclass