Memory management
Operating Systems ยท 10 interview questions
Memory management maps the addresses a program uses onto physical memory. Early contiguous allocation gave each process one block, which caused external fragmentation: enough free memory in total, but no single hole large enough.
Paging fixes that by splitting memory into fixed-size frames and the address space into pages of the same size. Any page fits any frame, so external fragmentation disappears entirely. What remains is internal fragmentation โ the unused remainder of the final page.
Segmentation instead divides the address space along logical lines โ code, stack, heap โ into variable-size segments. That matches how programs are structured and makes protection natural, but variable sizes bring external fragmentation back. Most real systems page, and some combine the two by paging within segments.
Memory management interview questions
- What is internal fragmentation?
- Space wasted inside an allocated block because the allocation is larger than the request โ the unused tail of the last page. It's the fragmentation paging causes.
- What is external fragmentation?
- Enough total free memory to satisfy a request, but no single contiguous hole big enough because free space is scattered. It's what variable-size allocation causes.
- Why they ask: The internal/external pairing with paging/segmentation is the whole point of this topic.
- Which fragmentation does paging eliminate, and which does it keep?
- It eliminates external fragmentation, because any page fits any frame. Internal fragmentation remains in the final partly-used page.
- How does segmentation differ from paging?
- Segments are variable-size and follow logical divisions like code, stack and heap, so they're meaningful to the programmer and easy to protect per-segment. Pages are fixed-size and purely physical. Segmentation causes external fragmentation; paging does not.
- What does a page table do?
- Maps virtual page numbers to physical frame numbers for a process, along with valid, dirty and permission bits. Every memory access consults it, which is why it's cached in the TLB.
- What is the TLB and why does it exist?
- A small associative cache of recent page-table entries. Without it every memory access would need an extra access to read the page table first, doubling the cost. A TLB hit makes translation effectively free.
- Why are page tables multi-level?
- A flat table for a 64-bit address space would be impossibly large. Multi-level tables only allocate the parts covering addresses actually in use, so sparse address spaces cost little, at the price of extra lookups on a TLB miss.
- What's the difference between a logical and a physical address?
- The logical (virtual) address is what the program generates; the physical address is the real memory location. The MMU translates between them at every access, which is what makes isolation and relocation possible.
- What is compaction and when can it be used?
- Moving allocated blocks together to merge free space into one usable hole. It only works when addresses are relocatable at run time, and it's expensive because everything must be copied.
- First fit, best fit, worst fit โ what's the trade-off?
- First fit takes the first hole big enough and is fastest. Best fit takes the smallest adequate hole, which minimises waste per allocation but leaves many tiny unusable fragments. Worst fit takes the largest, aiming to leave usable remainders. First fit generally performs best in practice.
You'll forget most of this by next week
That's not a discipline problem, it's how memory works. In the app these come back on an expanding schedule โ right before you'd lose them.
Start free for 7 days