View Course Path

Memory Management in Operating Systems – Simple Explanation

Understanding Memory Management in Operating Systems

Memory management stands as a pivotal function within an Operating System, falling under its primary role of resource management. Main memory, commonly known as RAM, serves as the backbone for running most applications. For consumers, the RAM often becomes a key consideration when purchasing a new phone or laptop.

Learn more about the two most important functions of an Operating System.

Computer Memory Hierarchy Explained

Ideally, users seek a private, limitless, ultra-fast memory that’s also nonvolatile, meaning it retains data even if the computer powers off unexpectedly. However, such a solution remains prohibitively expensive and beyond the current technological capabilities.

To address this, engineers have developed three distinct types of computer memory:

  • Cache Memory: This is a high-speed, small-sized memory that’s both expensive and volatile.
  • Random Access Memory (RAM): Offering medium capacity, ranging from a few gigabytes, RAM provides a balance of price, speed, and volatility.
  • HDD or SSD: These are large storage solutions, offering up to a terabyte of space, at a more affordable price. While slower, they’re nonvolatile.

The Role of Memory Manager in Operating Systems

The Operating System incorporates a crucial component known as the memory manager. This manager’s responsibilities encompass:

  • Monitoring the usage of memory segments.
  • Allocating memory resources to various processes.
  • Releasing memory once processes have completed their tasks.

Efficient memory management is paramount to prevent system deadlocks, where applications compete for resources, leading to system freezes. In the rare event of a deadlock, the Operating System possesses mechanisms to resolve such issues effectively.

Refresh your memory, read what deadlocks are before proceeding.

Understanding Memory Management Requirements in Operating Systems

In today’s computing landscape, processes demand substantial memory to operate efficiently. While memory sizes have expanded to offerings like 32GB, the growing size of applications necessitates optimal memory allocation for smooth performance. Effective memory management in an Operating System ensures continuous availability of memory for active processes.

Why Memory Management is Essential in Operating Systems

Memory management plays a pivotal role in ensuring system stability and efficiency. Here’s why it’s indispensable:

  • Relocation:
    • In a multiprogramming environment, various processes run concurrently. Predicting which programs will reside in the main memory or when processes will execute becomes challenging. The memory manager dynamically allocates and deallocates memory for executed and pending processes. This dynamic approach ensures seamless and efficient process execution.
  • Protection:
    • In a multi-process environment, one process may inadvertently access another process’s address space, leading to potential data corruption or system instability. Memory management safeguards each process’s address space, ensuring isolation and preventing unwanted interference. This protection mechanism operates in tandem with the relocation algorithm.
  • Sharing:
    • Efficient memory utilization demands a mechanism allowing multiple processes to access shared memory segments. Rather than allocating separate memory copies for each program, memory management facilitates controlled access to shared memory areas without compromising protection.
  • Logical Organization:
    • Memory presents a linear storage structure, comprising modifiable and non-modifiable data segments. Memory management orchestrates the allocation, utilization, and access of memory in a structured manner, preventing inadvertent modifications to critical files. It offers modular protection and sharing capabilities, with independent modules compiled to resolve runtime references.
  • Physical Organization:
    • Memory comprises volatile main memory (RAM) and non-volatile secondary memory (e.g., hard drives). While applications reside in secondary memory, they transition to RAM when executed. Effective memory management ensures seamless data transfers between main and secondary memory, optimizing system performance.

What is Memory Partitioning in OS?

Let’s move on to memory partitioning. For better utilization of memory and flow of execution, we divide the memory into different sections to be used by the resident programs. The process of dividing the memory into sections is called memory partitioning. There are different ways in which memory can be partitioned:

Fixed Partitioning/Static Partitioning

In fixed partitioning, the number of non-overlapping partitions in RAM is fixed, but the size of each partition may not be the same. As the allocation of memory is contiguous, no spanning is allowed. In fixed partitioning, the partitions are made either before execution or during system configuration.

Dynamic Partitioning

In dynamic partitioning, the primary memory is emptied, and partitions are made during the run time according to the needs of the different processes. The size of the partition will be equal to the incoming process. The number of partitions will not be fixed and will depend on the number of incoming processes. It will also depend on the size of the main memory. The partition size varies according to the need of the processes.

Buddy System

In static partitioning, we suffer from the limitation of having the fixed number of active processes, which at times leads to inefficient usage of space. The buddy system is a memory allocation and management algorithm that manages memory in power of two increments. For instance, if we have a memory of size 2U and we require a size of S, then if 2U-1<S<=2U, then it allocates the whole block,

  • else, recursively divide the block equally and test condition each time.
  • Buddy system also keeps the record of all the unallocated blocks and can merge different blocks too. It is easy to implement and allocates a block of the correct size, but it requires all allocation units to be powers of two.

Fragmentation in Operating Systems

After the partitioning of memory comes the fragmentation. When the processes are loaded to and removed from memory, the memory space that gets freed breaks into little pieces. Now, when this happens, no further processes can be allotted memory as their size becomes too small for that, and hence the memory blocks remain unused. The solution to this problem is known as fragmentation. In other words, fragmentation can be defined as the issue of a memory that arises when processes are loaded to and removed from the memory, breaking it into pieces. The fragmentation is mainly of two types:

  1. External fragmentation – In external fragmentation, the total memory space is enough to satisfy a request or to reside a process in it. But it is not contiguous, so it cannot be used.
  2. Internal fragmentation – In internal fragmentation, the memory block assigned to processes is bigger. Some portion of the memory may be left unused, as it cannot reside in any other process.

Swapping in OS

Swapping is a technique for making memory compact. It is a mechanism that is used to temporarily swap processes out of the main memory to secondary memory, and this makes more memory available for some other processes. At some later time, the system can swap back the process from the secondary memory to the main memory.

Swapping does affect the performance of the system, but it helps in running multiple processes parallelly. The total time taken by the swapping of a process includes the time it takes to move the entire process to the secondary memory and then again to the main memory.

Different types of Memory Management Techniques

The operating system has to manage free memory as well as do its necessary operations.

  • Managing free memory using a linked list
  • Managing free memory using bitmap
  • Memory management using paging
  • Memory management using segmentation
  • Memory management using virtual memory

When the memory is allocated and de-allocated dynamically, the operating system must be able to manage it. So, to keep track of memory usage, the operating system, generally, uses two ways:

Memory management using bitmap

In case of using a bitmap, the memory is firstly divided into allocation units, corresponding to each allocation unit, a bit is assigned in the bitmap. A bit is 0 if the unit is free, else it is 1. In this way of memory management, the allocation size is a design issue, as the number of bits increases along with the number of allocation units.

Memory management using a linked list

Another way to manage memory is to use a linked list to keep track of all the allocated and free memory segments. The segment list is kept sorted by address, making the swapping of processes easy. Each entry list specifies either a hole or a process, the starting address and the pointer next to the entry.

Memory Paging

With dynamic memory allocation, there is a possibility that the memory occupied can be non-contiguous. In that case, to manage memory efficiently, we use a technique called paging. Paging is a memory management technique which allows the memory allocation to be non-contiguous. Or in other words, the mapping from virtual to a physical address is known as the paging technique.

A page table is used to store the mapping in paging. A page table is a data structure used by the virtual memory to store the mapping between the logical and the physical addresses. CPU is responsible for generating logical addresses, and the processes generally use them. On the other hand, physical addresses are the actual frame addresses of the memory.

So, for instance, if

Physical Address Space = M words

Logical Address Space = L words

Page Size = P words

Physical Address = log 2 M = m bits

Logical Address = log 2 L = l bits

page offset = log 2 P = p bits, then the page table can be made as

Memory Segmentation

In an operating system, segmentation is a memory management technique in which the memory is divided into segments of variable size and are allocated to a process. The details about each segment are stored in a segment table, and the segment table is stored in one of the segments itself. The segment table contains only two pieces of information, namely base, which gives the base address of the segment and the limit, which tells about the length of the segment.

A CPU generated logical address has two parts, namely, segment number and offset. The segment number is mapped to the segment table. The segment is compared with the offset. If the offset is less than the limit address only, then the address is valid. And if the address is valid, the base address of the segment is added to the offset to get the physical address.


Virtual Memory

Virtual memory can be defined as a storage allocation scheme in which secondary storage is accessed as though it is a part of the main memory.

It is a technique that is implemented using both hardware and software. In the case of virtual memory, the size of virtual storage is limited by the addressing scheme of the computer, and the amount of secondary memory available. Virtual memory maps the memory addresses used by a program to physical addresses. It is implemented using Demand Paging or Demand Segmentation.

Related courses for this will be up soon!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.