Memory Management Interview Questions Operating System (original) (raw)

Last Updated : 1 Sep, 2025

Memory management in an operating system deals with the efficient allocation, tracking, and freeing of memory for processes. It covers techniques like paging, segmentation, and virtual memory to optimize performance and prevent fragmentation. The OS must balance speed, resource utilization, and protection while ensuring each process runs in isolation without interference.

1. What is memory management in an Operating System? What is the difference between logical and physical address?

Memory management is the process of handling computer memory efficiently by allocating, tracking, and freeing memory for programs and processes.

2. What is the difference between logical and physical address?

2. In a demand paging system, explain how “Belady’s Anomaly” can occur and why some algorithms are immune to it.

**Belady’s Anomaly in Demand Paging:

**Why It Happens (Example with FIFO):

**Example Reference String: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

**Why Some Algorithms Are Immune: Algorithms like LRU (Least Recently Used) and Optimal Replacement are immune because they are stack algorithms. Stack Property:

LRU -> Always removes the least recently used page (closer to optimal).
Optimal Replacement -> Removes the page that will not be used for the longest future time (theoretical best).

Both follow the principle of optimality, so no anomaly occurs.

3. Explain the difference between internal and external fragmentation with examples, and how paging/segmentation addresses each.

**Solutions:

Paging reduces external fragmentation but not internal.
Compaction can reduce external fragmentation but is costly.

**4. Explain the concept of Copy-on-Write (COW) and its advantages.

Copy-on-Write allows multiple processes to share the same memory page until one modifies it, at which point the OS makes a copy for the modifying process.

**Advantages:

**Use Case: OS process creation, virtual memory optimization.

5. Describe the working of the Translation Lookaside Buffer (TLB) and its effect on performance.

The TLB is a high-speed cache storing recent page table entries to speed up virtual-to-physical address translation.

**Working:

**Performance Impact: Reduces address translation time drastically. Hit ratios of 95%+ are common in modern systems.

6. Compare segmentation with paging in terms of protection, sharing, and memory efficiency.

Both segmentation and paging are memory management schemes in virtual memory systems, but they differ in how they divide memory and in their handling of protection, sharing, and efficiency.

Feature Segmentation Paging
**Unit of division Logical units (code, data, stack) Fixed-size pages
**Protection Per segment (logical boundaries) Per page (uniform)
**Sharing Easy, share segments across processes Possible via page table mapping
**Fragmentation External fragmentation Internal fragmentation
**Efficiency Matches program structure Simplifies allocation & avoids external fragmentation

7. How does the OS handle a “page fault” from the moment it occurs to process resumption?

**Steps:

  1. CPU detects page not present in memory -> triggers page fault trap to OS.
  2. OS checks if reference is valid (legal address). If invalid -> segmentation fault.
  3. If valid, find location on disk.
  4. Choose a free frame (or run page replacement if none free).
  5. Read page from disk into frame.
  6. Update page table and possibly TLB.
  7. Restart the instruction that caused the fault.

8. Why might an OS use both paging and segmentation together?

Combining them leverages the benefits of both:

Example: Intel x86 architecture uses segmentation at the top level, then pages each segment internally.

9. How can the OS dynamically adjust the degree of multiprogramming based on memory pressure?

**Dynamic Adjustment of Degree of Multiprogramming (DoM):

**How the OS Monitors Memory Pressure:

**Techniques for Dynamic Adjustment:

If PFF is above a threshold -> OS allocates more frames or suspends the process if memory is insufficient.
If PFF is below a threshold -> OS may reclaim some frames and allocate them to other processes.

10. Explain “memory-mapped files” and their advantages over traditional file I/O.

Memory-mapped files map a file’s contents directly into the process’s address space, allowing file data to be accessed via memory loads/stores instead of read/write syscalls.
**Advantages:

11. Explain the concept of “Belady’s Anomaly” with an example. Why does it occur in FIFO page replacement?

Belady’s Anomaly is a counterintuitive situation where increasing the number of page frames in memory results in more page faults, not fewer.

**Cause: Occurs in FIFO or FIFO -like page replacement policies because the algorithm doesn’t consider page usage frequency; it may remove a frequently used page just because it is the oldest.

**Example: Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

**With 3 frames: 9 page faults
**With 4 frames: 10 page faults

**Prevention: Use algorithms like LRU or Optimal Replacement that avoid this anomaly.

12. How does the OS handle memory protection and isolation in a multi-process environment?

Memory protection ensures that one process cannot access another’s memory space, maintaining security and stability. Mechanisms:

**OS Role: Performs checks during address translation and triggers a protection fault (segmentation fault) if a violation occurs.

13. Compare Internal Fragmentation and External Fragmentation with scenarios. How can each be minimized?

**Internal Fragmentation: Memory waste inside allocated blocks (e.g., allocating 1 KB when process needs only 700B).

**External Fragmentation: Free memory scattered into non-contiguous blocks, making it unusable despite enough total space.

14. In virtual memory, how do Translation Lookaside Buffers (TLB) impact performance, and what are the trade-offs of increasing TLB size?

The TLB caches recent virtual-to-physical address translations, reducing the need for frequent page table lookups, which is a overhead as page table is present in main memory. A high TLB hit ratio greatly improves effective memory access time.
**Trade-offs:

15. What is Demand Paging, and how does it differ from pure paging?

Demand paging loads a page into memory only when it is accessed, rather than preloading all pages.

**Advantages: Reduces initial load time and memory usage.

**Steps:

**Difference: Pure paging loads all pages upfront, which is slower and wastes memory if pages aren’t used.

16. Describe how “Copy-on-Write” works in virtual memory systems and why it’s important for process creation.

Copy-on-Write is an optimization technique in virtual memory systems used when processes share memory pages. Instead of immediately copying all pages during process creation (e.g., fork() in Unix/Linux), the OS delays copying until one of the processes modifies the page.

**How COW Works (Step-by-Step):

Allocates a new physical frame.
Copies the original page’s contents into the new frame.
Updates the page table of the writing process to point to the private copy.
The other process continues using the old page.

**Importance of COW:

17. How does the OS implement swapping, and what are its performance implications?

Swapping temporarily moves inactive processes from main memory to disk to free up space for active ones.

**Steps:

**Performance Impact: Increases CPU availability but can be slow due to disk I/O overhead (swap thrashing if excessive).

18. Discuss the concept of Memory-Mapped Files. How does it improve I/O performance?

**Concept of Memory-Mapped Files:

**How Memory-Mapped Files Work:

If the corresponding page is not yet in RAM -> page fault occurs.
The OS loads the needed page from disk into memory (on-demand paging).

**How It Improves I/O Performance:

19. Explain the difference between Inverted Page Table and Conventional Page Table.

In conventional Page Table, Each process has a page table with one entry per virtual page -> large memory usage for large address spaces. While, In inverted Page Table: Single global table with one entry per physical frame -> smaller size.

Feature Conventional Page Table Inverted Page Table
**Structure One table per process One global table for the system
**Entries One entry per _virtual page One entry per _physical frame
**Memory Usage Very large for big virtual address spaces Much smaller, depends only on physical memory
**Lookup Speed Fast, direct indexing Slower, requires search (hashing used)
**Process Isolation Each process has its own page table Uses (PID, VPN) to distinguish processes
**Overhead High memory overhead Higher computational overhead

**Trade-off: Inverted tables save memory but require more complex lookups, often needing hashing.

20. What is Thrashing in OS memory management, and how can it be prevented?

Thrashing is a condition in a virtual memory system where the CPU spends most of its time swapping pages in and out of memory(handling page faults) instead of executing instructions. As a result -> system performance drops drastically.

**Cause:

**Symptoms of Thrashing:

**Prevention: