W8 - Memory Management

Layered Memory

Today we will focus on the management of the main memory (RAM)

image-47.png354

Why Does Memory Need Management?

Protection

Sharing

Abstraction

A Historical Perspective: Early Computers

Very limited memory and lacked the sophisticated memory protection and
address translation used in modern general-purpose systems.

No Memory Abstraction

What Could Go Wrong Without Isolation?

Notes:

Memory Protection Keys

Notes:

Memory Abstraction: Address Space

Notes:

Can Two Processes Use the Same Address?

Same Address. Different Memory?

Can both processes use the same address (0x1000)?

Process Creation - fork() example

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
	int *A = malloc(sizeof(int));
	*A = 50;
	
	pid_t pid = fork();
	
	*A = *A + 1;
	
	printf("PID=%d, address=%p, A=%d\n", getpid(), (void *)A, *A);
	return 0;
}

Output:

PID=43913, address=0x105581a00, A=51
PID=43914, address=0x105581a00, A=51

Dynamic Relocation

image-48.png193x276

Notes:

Address Translation

Example:

What happens when the process accesses logical address is 2500?

What happens when the process accesses logical address is 4500?

Memory Overload

What Happens When RAM is Full?

Notes:

Managing Free Memory: Bitmaps

Notes:

Managing Free Memory: (Double) Linked List

Notes:

Challenge

Fragmentation: The Hidden Problem

There are 5 free units. Can we allocate a contiguous block of 3?

image-51.png479x36

There Is Enough Free Memory. Why Can't We Allocate?

Virtual Memory

The Birth of Virtual Memory:

image-52.png292x285

Memory Management Unit (MMU)

Virtual Memory: Paging Example

image-54.png188

Let's translate the address (example):

Given virtual address 10000:

 Page number =⌊10000/4096⌋=2 Offset =10000mod4096=1808

If page 2 maps to physical frame 6:

Physical address=6×4096+1808=26384

Virtual Memory: Page Tables

image-56.png324x359

Large Page Tables

image-57.png331x456

Three things to remember

  1. Address spaces provide abstraction and isolation between processes.
  2. The MMU and page tables translate virtual addresses into physical addresses.
  3. Virtual memory allows execution without keeping every page in RAM

Why do we need a Translation Lookaside Buffer (TLB)?

The Translation Lookaside Buffer (TLB)

image-58.png328

TLB Hit vs TLB Miss

What happens when the CPU accesses memory?

image-59.png328x206

Anatomy of a Page Fault

When a memory access cannot proceed

  1. The CPU raises a page-fault exception.
  2. The OS examines the address and the reason for the fault.
  3. If the access is valid, the OS resolves the fault (possibly loading or allocating a page).
  4. The instruction is restarted and execution continues

image-60.png330x281

A Page Fault is not Necessarily an Error

Demand Paging on macOS (example):

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>

int main(void) {
	size_t page = (size_t)sysconf(_SC_PAGESIZE);
	size_t pages = 16384;
	size_t bytes = pages * page;
	
	char *p = mmap(NULL, bytes, PROT_READ | PROT_WRITE,
				   MAP_PRIVATE | MAP_ANON, -1, 0);
	
	if (p == MAP_FAILED) {
		perror("mmap");
		return 1;
	}
	
	printf("Page size: %zu bytes\n", page);
	printf("Reserved: %zu MiB\n", bytes / 1024 / 1024);
	
	printf("Press Enter to touch every page...\n");
	getchar();
	
	for (size_t i = 0; i < pages; i++)
		p[i * page] = 1;
	
	printf("All pages touched. Press Enter...\n");
	getchar();
	
	munmap(p, bytes);
	return 0;
}

Output:

[robertomorabito@Mac W6-Memory Management % ps -o pid,rss,vsz,comm
    PID RSS VSZ COMM
59909 1440 435561616 ./demand_paging
[robertomorabito@Mac W6-Memory Management % ps -o pid,rss,vsz,comm
    PID RSS VSZ COMM
59909 263584 435561616 ./demand_paging

The Replacement Problem

Suppose physical memory has only three frames, currently containing
pages A, B, and C.

A process requests page D. Which page should the OS evict?

Page Replacement Algorithms

Not Recently Used (NRU)

Class R M Interpretation
0     0 0 Not recently used, clean
1     0 1 Not recently used, dirty
2     1 0 Recently used, clean
3     1 1 Recently used, dirty

Limitations?

First-In, First-Out (FIFO)

Belady's Anomaly

Reference sequence
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

Under FIFO:
image-62.png412x35

Notes:

Second Chance

Clock

Notes:

Least Recently Used (LRU)

Why isn't exact LRY used everywhere?

Not Frequently Used (NFU)

Ageing

image-64.png429x228

Which Algorithm Would You Choose?

Scenario Useful idea
Simplest implementation FIFO
Recent references should matter LRU
Approximate recency with low overhead Clock
Track recent access history Ageing

The best replacement policy is a trade-off between implementation overhead and replacement effectiveness.

Working Set

image-65.png347x217

Notes:

Thrashing and Why it Happens

image-66.png346x119

Thrashing occurs when insufficient resident memory causes excessive paging activity, leaving little useful progress.

WSClock

image-67.png275x307

Summary of Page Replacement Algorithms

image-13.png475

What does macOS do today?

Three Things to Remember

  1. Virtual memory separates the addresses a program uses from physical RAM.
  2. Page replacement is a prediction problem: which pages will be useful again?
  3. Locality is the reason virtual memory can work efficiently.