7 Memory Management

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.

Memory Management: notes

Virtual memory technique and purpose

Virtual memory lets a program run even if it is larger than the physical memory. Only the parts needed now are kept in RAM. The rest stays on disk. The user sees a very large, simple address space.

Purposes:

  • Run programs bigger than RAM.
  • Keep more programs in memory at once, so the CPU is used better.
  • Give each process its own private address space.
  • Free the programmer from worrying about memory size.

It is usually built with paging or segmentation, as explained in the next chapter.

Logical address definition

A logical address (virtual address) is the address that the CPU generates when a program runs. The program sees only logical addresses. The set of all of them is the logical address space.

A logical address starts at 0 for each process. It does not point directly to a real place in RAM.

Physical address conversion

A physical address is the real address of a location in main memory. The set of all of them is the physical address space.

The memory management unit (MMU) is hardware that maps a logical address to a physical address at run time.

A simple method uses a relocation register (base register).

$$\text{Physical address} = \text{Logical address} + \text{Base}$$

Example: base = 14000 and logical address = 346.

$$\begin{aligned} \text{Physical} &= 346 + 14000 \ &= 14346 \end{aligned}$$

A limit register holds the size of the process. If the logical address is larger than the limit, the hardware raises an error. This protects other processes.

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]

Both pure segmentation and pure paging have their own limitations. To overcome these limitations, modern operating systems combine both techniques into a hybrid scheme called Segmentation with Paging. --- Pure Paging Pure Segmentation ------ No logical grouping of program Logical grouping is pres...

Open this answer on its own page →

Coalescing and compaction techniques

2080 · 5 marks

Write down the basic difference between coalescing and compaction with diagram. [5]

--- Definition: Coalescing is the process of merging two or more adjacent free memory blocks into a single larger free block when a block is deallocated. - It happens immediately when a block is freed and its neighbor(s) are also free. - It only combines physically adjacent free holes. - It does ...

Open this answer on its own page →

Memory compaction

2079 · 5 marks

What is memory compaction? Explain best fit memory allocation with suitable example. [5]

Show solved 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):

HoleSize
H1100 KB
H2500 KB
H3200 KB
H4300 KB
H550 KB

Process Requests:

ProcessMemory Required
P1212 KB
P2417 KB
P3112 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:

HoleOriginalRemaining
H1100 KB100 KB (unused)
H2500 KB83 KB (leftover)
H3200 KB88 KB (leftover)
H4300 KB88 KB (leftover)
H550 KB50 KB (unused)

Notice that best fit leaves behind many small leftover fragments, which is its main drawback despite minimizing waste per allocation.

Open this answer on its own page →

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]

Note: Reference notes were not available for this topic. The answer below is based on standard Operating Systems curriculum as taught in TU BSc CSIT (consistent with Silberschatz/Galvin and similar texts used in the program). --- Virtual memory is a memory management technique that allows a proce...

Open this answer on its own page →

Swapping definition

0 · 5 marks

Define swapping. How does the OS manage the virtual memory? [5]

--- Swapping is a memory management technique in which a process (or a portion of a process) is temporarily moved out of main memory (RAM) to a backing store (usually a hard disk or swap space), and later brought back into memory for continued execution. - The process moved out is said to be swap...

Open this answer on its own page →