📖 Scenario: You are learning how computer memory is managed. When programs use memory, sometimes space is wasted. This wasted space is called fragmentation. There are two main types: internal and external fragmentation.Imagine a parking lot with fixed-size parking spots. Cars that are smaller than the spot leave empty space inside the spot (internal fragmentation). If the parking spots are scattered and not continuous, some cars cannot park even if there is enough total space (external fragmentation).
🎯 Goal: Build a simple example that shows how internal and external fragmentation happen in memory allocation. You will create a list of memory blocks, mark their sizes, and then simulate allocation to see wasted space inside blocks (internal) and leftover unusable space between blocks (external).
📋 What You'll Learn
Create a list of memory blocks with fixed sizes
Set a variable for the requested memory size
Write code to find a block that fits the request and calculate internal fragmentation
Calculate external fragmentation as total free space in unusable small blocks
💡 Why This Matters
🌍 Real World
Memory management is a key part of operating systems. Understanding fragmentation helps in designing efficient memory allocation.
💼 Career
Operating system developers, system programmers, and software engineers benefit from knowing how fragmentation affects performance and resource use.
Progress0 / 4 steps
1
Create memory blocks list
Create a list called memory_blocks with these exact integer sizes in KB: 100, 500, 200, 300, 600.
Operating Systems
Hint
Use square brackets to create a list and separate numbers with commas.
2
Set requested memory size
Create a variable called requested_size and set it to 212 to represent the memory size requested by a program.
Operating Systems
Hint
Assign the number 212 to the variable requested_size.
3
Find block and calculate internal fragmentation
Write a for loop using variables index and block_size to iterate over memory_blocks with enumerate(). Inside the loop, find the first block where block_size is greater than or equal to requested_size. Then create a variable internal_frag that is the difference between block_size and requested_size. Stop the loop after finding this block.
Operating Systems
Hint
Use enumerate(memory_blocks) to get index and block size. Use break to stop after first fit.
4
Calculate external fragmentation
Create a variable called external_frag that sums all block sizes in memory_blocks which are less than requested_size. Use a list comprehension inside the sum() function.
Operating Systems
Hint
Use a list comprehension to filter blocks smaller than requested_size and sum them.
Practice
(1/5)
1. Which of the following best describes internal fragmentation in memory management?
easy
A. Wasted space outside allocated memory blocks
B. Wasted space inside allocated memory blocks
C. Memory lost due to hardware failure
D. Memory used by the operating system kernel
Solution
Step 1: Understand internal fragmentation
Internal fragmentation happens when allocated memory blocks have unused space inside them because the block size is larger than the requested memory.
Step 2: Compare with external fragmentation
External fragmentation is about wasted space outside allocated blocks, so it is different from internal fragmentation.
Final Answer:
Wasted space inside allocated memory blocks -> Option B
Quick Check:
Internal fragmentation = wasted space inside blocks [OK]
Hint: Internal = inside allocated blocks wasted space [OK]
Common Mistakes:
Confusing internal with external fragmentation
Thinking fragmentation is caused by hardware
Mixing up allocated and free memory spaces
2. Which of the following statements about external fragmentation is syntactically correct?
easy
A. External fragmentation wastes space inside allocated blocks.
B. External fragmentation is caused by software bugs only.
C. External fragmentation means memory is fully used without gaps.
D. External fragmentation wastes space outside allocated blocks in free memory.
Solution
Step 1: Identify correct definition of external fragmentation
External fragmentation refers to wasted space outside allocated blocks, scattered in free memory.
Step 2: Check other options for correctness
The statement "External fragmentation wastes space inside allocated blocks" describes internal fragmentation. The claim that it is caused by software bugs only is incorrect, as it arises from repeated allocation and deallocation. The idea that memory is fully used without gaps is false, since fragmentation creates unusable gaps in free memory.
Final Answer:
External fragmentation wastes space outside allocated blocks in free memory. -> Option D
Hint: External = wasted space outside allocated blocks [OK]
Common Mistakes:
Mixing internal and external fragmentation definitions
Assuming fragmentation is only due to bugs
Ignoring free memory gaps in external fragmentation
3. Consider a memory system where 5 blocks of size 10KB are allocated, but each request was for 8KB. What type of fragmentation is present and what is the total wasted memory?
medium
A. Internal fragmentation; 10KB wasted total
B. Internal fragmentation; 2KB wasted
C. Internal fragmentation; 10KB wasted inside each block
D. External fragmentation; 10KB wasted
Solution
Step 1: Calculate wasted space per block
Each block is 10KB but only 8KB is used, so 2KB is wasted inside each block.
Step 2: Calculate total wasted memory
With 5 blocks, total wasted = 5 blocks * 2KB = 10KB wasted inside allocated blocks, which is internal fragmentation.
Final Answer:
Internal fragmentation; 10KB wasted total -> Option A
Quick Check:
Internal fragmentation = wasted inside blocks total 10KB [OK]
4. A system has free memory blocks of sizes 5KB, 3KB, and 4KB scattered around. A process requests 6KB. Which problem is this an example of, and what is the main issue?
medium
A. External fragmentation; no single free block large enough
B. Internal fragmentation; wasted space inside allocated blocks
C. External fragmentation; all free blocks are too large
D. Internal fragmentation; process size is too small
Solution
Step 1: Analyze free memory blocks
The free blocks are 5KB, 3KB, and 4KB. The process needs 6KB, but no single block is large enough (maximum 5KB < 6KB).
Step 2: Identify fragmentation type and issue
Free memory total is 12KB > 6KB, but scattered into small non-contiguous blocks with none large enough, causing allocation failure. This is external fragmentation.
Final Answer:
External fragmentation; no single free block large enough -> Option A
Quick Check:
External fragmentation = scattered free blocks too small [OK]
Hint: External fragmentation = scattered free blocks too small [OK]
Common Mistakes:
Confusing internal with external fragmentation
Assuming process size is too small causes fragmentation
Ignoring block sizes when allocating memory
5. A system uses fixed-size memory blocks of 4KB. A program requests 10KB. How does internal and external fragmentation affect this allocation, and what is the total wasted memory?
hard
A. Internal fragmentation wastes 4KB; external fragmentation wastes 2KB
B. Internal fragmentation wastes 2KB; external fragmentation wastes 4KB
C. Internal fragmentation wastes 2KB; external fragmentation wastes 0KB
D. Internal fragmentation wastes 0KB; external fragmentation wastes 2KB
Solution
Step 1: Calculate blocks needed and internal fragmentation
10KB requires 3 blocks of 4KB each (total 12KB). The extra 2KB (12KB - 10KB) inside allocated blocks is internal fragmentation.
Step 2: Consider external fragmentation
Since blocks are fixed and allocated contiguously, no free space outside allocated blocks is wasted here, so external fragmentation is 0KB.