๐Ÿง  Memory Management

๐ŸŽฏ Why Memory Management?

๐Ÿ’ฅ Problem:

Multiple processes want to use the limited physical memory (RAM).

Goal:

  • โœ… Keep CPU busy
  • โœ… System should stay responsive
  • โœ… Protect one process from accessing anotherโ€™s memory

Operating System

Always stays in memory

Process A

Running program

Process B

Waiting or ready to run

Free Memory

Unused RAM available

๐Ÿ  Logical vs Physical Address

๐Ÿง  Logical Address

๐Ÿ“ฑ Created by CPU

๐Ÿ‘€ What programmer sees (starts from 0)

๐Ÿ’ญ Example: 0, 1, 2, 3...

๐ŸŒ€ Fake/Virtual address

๐Ÿข Physical Address

๐Ÿ“ Actual location in RAM

๐Ÿ”’ Hidden from programmer

๐Ÿงฎ Calculated using Base Register

โœ… Real memory address like 1000, 1001...

๐Ÿ”„ Address Translation with Base Register

๐Ÿ› ๏ธ MMU (Memory Management Unit) converts Logical โ†’ Physical

Formula:

Physical Address = Base Register + Logical Address

Example: Base = 1000, Logical = 25 โ†’ Physical = 1025

Address Mapping

๐Ÿ›ก๏ธ Memory Protection - Base & Limit Registers

How OS Protects Memory:

๐Ÿ  Base Register

Starting address in physical memory (e.g. 1000)

๐Ÿ“ Limit Register

Total size allowed (e.g. 500 bytes)

Rule: CPU checks every memory access using:

0 โ‰ค Logical Address < Limit Register

If valid โžก๏ธ Then: Physical = Base + Logical

// Memory Protection Check (hardware logic)
if (logical_address >= limit_register) {
    TRAP_TO_OS(); // โŒ Illegal memory access!
} else {
    physical_address = base_register + logical_address;
}
    

๐Ÿ’ก Interview Tip:

Q: What if a process tries to access memory beyond its limit?

A: CPU generates a trap โžก๏ธ OS catches it โžก๏ธ Process is terminated โžก๏ธ โ€œSegmentation Faultโ€

๐Ÿ“ฆ Memory Allocation Methods

Contiguous vs Non-Contiguous Allocation

Two Main Approaches:

๐ŸŸฆ Contiguous Allocation

Process memory is in one continuous block

๐Ÿ“ Like parking in adjacent spots

๐ŸŸจ Non-Contiguous Allocation

Process memory is in scattered blocks

๐Ÿ“ Like parking in separate spots

๐Ÿ”ง Contiguous Allocation Types

๐ŸŽฏ Free Space Allocation Algorithms

The Problem:

after Complete the process, they live and hole is create,then allocate og hole is call free space management

๐Ÿš€ First Fit

   First Fit Algorithm

Strategy: Pick first hole that fits

Speed: โšก Fastest

Memory Use: ๐ŸŸก Average

๐ŸŽฏ Best Fit

Best Fit Algorithm

Strategy: Pick smallest hole that fits

Speed: ๐ŸŒ Slowest

Memory Use: ๐ŸŸข Best (less internal frag)

๐Ÿ’ฅ Worst Fit

Worst Fit Algorithm

Strategy: Pick largest hole that fits

Speed: ๐ŸŒ Slow

Memory Use: ๐Ÿ”ด Worst

โญ๏ธ Next Fit

Next Fit Algorithm

Strategy: First fit, but start from last allocated

Speed: โšก Fast

Memory Use: ๐ŸŸก Average

๐Ÿ’ก Interview Tip:

Q: "Which algorithm is best?"

A: "Depends on requirements. First Fit for speed, Best Fit for memory efficiency. Generally, First Fit and Best Fit are most commonly used."

๐Ÿงน Compaction/Defragmentation

Compaction in Operating System

What is Compaction?

Moving all allocated partitions together and all free space together

โœ… Benefits:

  • Eliminates external fragmentation
  • Creates large free blocks
  • Allows larger processes to load

โŒ Costs:

  • Expensive (copy all process data)
  • System stops during compaction
  • Need to update all pointers

๐Ÿ’ก Interview Tip:

Q: "When should we do compaction?"

A: "When external fragmentation becomes severe and no free block can satisfy new requests, but total free space is sufficient."

โšก Quick Revision Points

๐Ÿ”‘ Key Concepts to Remember:

  • โœ… Logical Address: CPU generates, programmer sees, virtual
  • โœ… Physical Address: Real memory location, hardware uses
  • โœ… MMU: Translates logical to physical (Base + Logical)
  • โœ… Fixed Partitioning: Simple but internal fragmentation
  • โœ… Dynamic Partitioning: No internal frag but external frag
  • โœ… Internal Fragmentation: Wasted space inside partition
  • โœ… External Fragmentation: Scattered free space
  • โœ… First Fit: Fastest algorithm
  • โœ… Best Fit: Least memory waste
  • โœ… Compaction: Solves external fragmentation

๐ŸŽฏ Common Interview Questions:

  • Q: "Why can't we access physical address directly?"
  • A: "For security, isolation, and dynamic memory management."

  • Q: "How does OS prevent one process from accessing another's memory?"
  • A: "Using base and limit registers. MMU checks every access."

  • Q: "What's the difference between internal and external fragmentation?"
  • A: "Internal = waste inside partition, External = scattered free space."