Vaidikalaya

Memory Management in Operating System


Memory management in an operating system is the process of managing the computer’s memory (RAM) — giving memory to programs when they need it and taking it back when they are done, so that all programs can run smoothly without disturbing each other.


Key Functions of Memory Management

  • Allocation: Assign memory blocks to processes when they need it.
  • Deallocation: Release memory once a process finishes, so it can be reused.
  • Protection: Ensure one process cannot access or corrupt another process’s memory.
  • Sharing: Allow controlled sharing of memory between processes when needed.
  • Relocation: Move processes in memory (logical vs. physical addresses).
  • Tracking: Keep records of which parts of memory are free or in use.

Why Memory Management is Required?

Memory management is required in an Operating System because without it, processes and applications could not run efficiently, securely, or even correctly.

  • Efficient utilization of limited memory (avoid wastage).
  • Process isolation and protection (prevent one process from interfering with another).
  • Enables multiprogramming and multitasking.
  • Avoids fragmentation and maximizes memory usage.
  • Supports virtual memory (use of disk space as extra RAM).
  • Handles relocation and logical → physical address mapping.
  • Improves CPU utilization by keeping processes ready to run.
  • Provides controlled memory sharing between processes.
  • Ensures system stability and prevents crashes due to memory conflicts.

Types of address in memory management

In Memory Management, there are mainly two types of addresses And exists only during program execution.

Logical Address (Virtual Address)

A Logical Address is the address generated by the CPU during program execution.

  • It is also called a Virtual Address because it does not directly refer to the physical RAM location.
  • Every process has its own logical address space, which gives the illusion that it has the whole memory to itself.
  • These addresses are later translated into actual Physical Addresses by the Memory Management Unit (MMU).
Physical Address

A Physical Address is the actual location in the main memory (RAM) and directly used by hardware to fetch/store data.

  • It is the address that is seen by the memory hardware.
  • Generated after the Memory Management Unit (MMU) translates a Logical Address.
  • It is unique for each memory cell and used to access/store data in RAM.

Steps of Executing a Program in Memory Management

1. Program Loading
  • When you run a program, it is loaded from secondary storage (disk) into main memory (RAM).
  • The OS allocates a portion of memory for it (using paging/segmentation/contiguous allocation).
2. Program Counter (PC) Setup
  • The CPU sets the PC (Program Counter) to the starting logical address of the program.
  • The PC always points to the next instruction’s logical address.
3. Address Translation
  • The CPU generates a logical address.
  • This logical address goes into the Memory Management Unit (MMU).
  • The MMU translates it into a physical address using mapping schemes (paging, segmentation, or both).
4. Instruction Fetch
  • Using the physical address, the CPU fetches the instruction from RAM.
  • The fetched instruction is stored in the Instruction Register (IR).
5. Instruction Execution
  • The CPU decodes and executes the instruction.
  • If the instruction needs data (e.g., from variables/arrays), the same logical → physical translation happens for those addresses.
6. Program Counter Update
  • The PC increments to point to the next logical address.
  • Steps 3–5 repeat until the program finishes.
7. Deallocation
  • When the program ends, the OS frees the allocated memory so it can be reused by other processes.
Example:
  • PC = 0x0020 (logical)
  • CPU sends to MMU → MMU translates to 0x4020 (physical)
  • RAM at 0x4020 has instruction → CPU executes it.
  • PC → moves to next logical address (0x0021).

Program Loading in OS

Program Loading is the process of bringing a program from secondary storage (like Hard Disk/SSD) into main memory (RAM) so that it can be executed by the CPU. A program stored on disk is just a file (with instructions + data). It cannot run directly from disk — it must be loaded into RAM first, because the CPU can only execute instructions from memory.

Types of loading

Static Loading 
  • Entire program is loaded into memory (RAM) at once before execution starts.
  • Once loaded, execution begins without further loading.
  • Fast, but requires enough RAM to hold the whole program.
Dynamic Loading
  • Only the required parts of a program are loaded into memory initially.
  • Other parts (modules, functions, or libraries) are loaded on demand at runtime.
  • Common in large programs and modern OS.
  • Saves memory, improves efficiency (used in large programs, libraries, OS modules).

Swapping 

Swapping is a memory management technique where a process (or part of it) is moved between main memory (RAM) and secondary storage (disk, usually swap space or page file) to ensure better utilization of CPU and RAM.

Conditions for Swapping
  • When RAM is full and a new process arrives.
  • When multiprogramming is used (several programs compete for memory).
  • Idle processes are swapped out to make room for active ones.
Example:
  • Suppose your computer has 4 GB RAM, but you open apps that require 6 GB.
  • The OS swaps out some inactive parts of programs to disk, freeing RAM for active ones.
  • When you return to the swapped-out app, it may take a little time to load back — this delay is caused by swapping.
Modern systems mostly use paging with virtual memory instead of whole-process swapping.

Memory Management Techniques

Memory management techniques are methods used by an operating system to efficiently allocate, utilize, and manage memory resources for processes. These techniques ensure that processes run smoothly, memory is used effectively, and the system remains stable.

They can be broadly categorized into two main types:

1. Contiguous Memory Allocation

In this method, each process is allocated a single continuous block of memory.

a) Fixed Partitioning
  • Memory is divided into fixed-size partitions.
  • Each partition holds exactly one process.
  • Simple, but may cause internal fragmentation.
b) Dynamic Partitioning
  • Partitions are created dynamically, based on process size.
  • Reduces internal fragmentation but may cause external fragmentation.
2. Non-Contiguous Memory Allocation

Here, a process is divided into parts that can be stored in different places in memory.

a) Paging
  • Memory is divided into fixed-size pages and frames.
  • Pages of a process can be stored in non-contiguous frames.
  • Solves external fragmentation.
b) Segmentation
  • Memory is divided based on logical segments (like code, data, stack).
  • Each segment can vary in size.
  • Provides logical view but may suffer external fragmentation.
c) Virtual Memory
  • Combination of paging + secondary storage.
  • Allows execution of processes larger than physical RAM.
  • Gives illusion of large memory using swap space.

Memory Management in OS is essential for efficient utilization of memory, process protection, multitasking, and smooth execution of programs by mapping logical addresses to physical addresses.