Operating Systems · Unit 7
Memory Management
Exam-focused notes for Memory Management (Operating Systems, BIT204): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.
Memory Management in short
Memory management decides how main memory is shared between processes. The OS must track free and used memory, give memory to processes, and protect each process from the others.
What this unit covers
- Virtual memory technique and purpose
- Logical address definition
- Physical address conversion
- Single level paging scheme
- Segmentation with paging
- Swapping definition
- Memory compaction
- Best fit memory allocation
- Coalescing and compaction techniques
Segmentation with paging
2082 · 5 marks
Why do we prefer segmentation with paging? Explain. [5]
Coalescing and compaction techniques
2080 · 5 marks
Write down the basic difference between coalescing and compaction with diagram. [5]
Memory compaction
2079 · 5 marks
What is memory compaction? Explain best fit memory allocation with suitable example. [5]
Show solved answerHide answer
Memory Compaction and Best Fit Memory Allocation
Memory Compaction
Memory compaction is a memory management technique used to overcome the problem of external fragmentation. When processes are loaded and removed from memory over time, free memory becomes scattered in small, non-contiguous blocks. Even if the total free memory is sufficient to accommodate a new process, no single contiguous block may be large enough.
In compaction, the OS shuffles all occupied memory partitions together (toward one end of memory), merging all the scattered free holes into one large contiguous free block.
Key Points:
- Requires dynamic relocation (base register support)
- It is time-consuming and costly in terms of CPU overhead
- Not always possible if relocation cannot be done at runtime
Before Compaction: After Compaction:
| P1 (20KB) | | P1 (20KB) |
| Free (10KB)| | P2 (15KB) |
| P2 (15KB) | ----> | P3 (5KB) |
| Free (8KB) | | Free (33KB)|
| P3 (5KB) | | |
| Free (15KB)| | |
Best Fit Memory Allocation
Best Fit is a contiguous memory allocation strategy in which the OS searches the entire list of free memory holes and allocates the smallest hole that is large enough to satisfy the process's memory request.
Idea:
Allocate the hole that leaves the least leftover free space after allocation.
Advantages:
- Minimizes wasted space in each individual allocation
- Leaves larger holes available for bigger future requests
Disadvantages:
- Slower -- must search the entire free list every time
- Tends to create many tiny, unusable leftover fragments (small holes that are too small for any process) -- leading to external fragmentation
Example
Free Memory Holes (initially):
| Hole | Size |
|---|---|
| H1 | 100 KB |
| H2 | 500 KB |
| H3 | 200 KB |
| H4 | 300 KB |
| H5 | 50 KB |
Process Requests:
| Process | Memory Required |
|---|---|
| P1 | 212 KB |
| P2 | 417 KB |
| P3 | 112 KB |
Step 1: Allocate P1 (212 KB)
Search all holes for the smallest hole >= 212 KB:
- H1 = 100 KB -- too small
- H2 = 500 KB -- fits, leftover = 288 KB
- H3 = 200 KB -- too small
- H4 = 300 KB -- fits, leftover = 88 KB <-- smallest leftover
- H5 = 50 KB -- too small
Best Fit: H4 (300 KB) Remaining in H4 = 300 - 212 = 88 KB
Step 2: Allocate P2 (417 KB)
Updated holes: H1=100, H2=500, H3=200, H4=88, H5=50
- H2 = 500 KB -- fits, leftover = 83 KB <-- only hole large enough
Best Fit: H2 (500 KB) Remaining in H2 = 500 - 417 = 83 KB
Step 3: Allocate P3 (112 KB)
Updated holes: H1=100, H2=83, H3=200, H4=88, H5=50
- H1 = 100 KB -- too small
- H2 = 83 KB -- too small
- H3 = 200 KB -- fits, leftover = 88 KB <-- smallest leftover
- H4 = 88 KB -- too small
- H5 = 50 KB -- too small
Best Fit: H3 (200 KB) Remaining in H3 = 200 - 112 = 88 KB
Final State of Memory:
| Hole | Original | Remaining |
|---|---|---|
| H1 | 100 KB | 100 KB (unused) |
| H2 | 500 KB | 83 KB (leftover) |
| H3 | 200 KB | 88 KB (leftover) |
| H4 | 300 KB | 88 KB (leftover) |
| H5 | 50 KB | 50 KB (unused) |
Notice that best fit leaves behind many small leftover fragments, which is its main drawback despite minimizing waste per allocation.
Virtual memory technique and purpose
2078 · 10 marks
Why virtual memory technique is used in the computer system? What is logical address? Explain the process of conversion of logical address to physical address in single level paging scheme.[10]
Swapping definition
0 · 5 marks
Define swapping. How does the OS manage the virtual memory? [5]
Make Unit 7 stick
Practice BIT204 with flashcards & quizzes