Introduction to Memory Management
Space v. Time Multiplexing
-
Example: CPU scheduling on a single-core system.
-
Example: Room scheduling using temporal scheduling.
-
Example: Car share programs.
-
Example: CPU scheduling on a multi-core system. Low granularity.
-
Example: memory management. High granularity.
-
Example: splitting up a cake.
Clarification: Memory Allocation
-
A process requests large chunks of memory from the OS kernel…
-
…and then divides available memory up using a process-level allocation library (such as
malloc
).
-
You’re probably more familiar with the second step, but we’re going to focus on the first. (Although allocators are fascinating and still an area of active research…)
Direct Multiplexing: Problems
-
Limited to the amount of physical memory on the machine.
-
What happens if processes request memory that they do not use?
Direct Multiplexing: Problems
-
Limited to the amount of physical memory on the machine.
-
Potentially discontiguous allocations.
-
Complicates process memory layout.
-
-
Potential for fragmentation to reduce allocation efficiency.
Process Memory Layout
How do processes know where their code and data is located?
int data[128];
...
data[5] = 8; // Where the heck is data[5]?
...
result = foo(data[5]); // Where the heck is foo?
Fragmentation
Fragmentation: when a request for contiguous memory fails despite the fact that there is enough unused memory available (on the system).
-
Internal fragmentation: unused memory is inside existing allocations.
-
External fragmentation: unused memory is between existing allocations.
Direct Multiplexing: Problems
-
Limited to the amount of physical memory on the machine.
-
Potentially discontiguous allocations.
-
Potential for fragmentation to reduce allocation efficiency.
-
Can I enforce my allocations?
-
Not without checking every memory access. Way too slow, but hardware could help…
-
-
Can I safely reclaim unused memory?
-
Leads to increased discontiguity and suffers from the same enforcement problem.
-
Memory Multiplexing Requirements
-
Grant: the kernel should be able to allocate memory to processes statically (at startup) and dynamically (as needed).
-
Enforce: the kernel should be able to enforce memory allocations efficiently.
-
Reclaim: the kernel should be able to repurpose unused memory without destroying its contents.
-
Revoke: the kernel should be able to stop a process from using memory that it was previously allocated.
Comparison to CPU
-
Grant: schedule a thread via a context switch.
-
Enforce: interrupt a thread using a timer interrupt.
-
Reclaim: this is new.
-
Revoke: deschedule a thread via a context switch.
Address Spaces: The Memory Management Abstraction
We provide every process with an identical view of memory that makes it appear:
-
plentiful, contiguous, uniform, and private.
Address Spaces: Layout
The uniformity of address spaces simplifies process layout:
-
"I always put my code and static variables at 0x10000."
-
"My heap always starts at 0x20000000 and grows up."
-
"My stack always starts at 0xFFFFFFFF and grows down."
Convention
-
Process layout is specified by the Executable and Linker Format (ELF) file. (Remember ELF?)
-
Some layout is the function of convention.
-
Example: why not load the code at
0x0
?-
To catch possibly the most common programmer error:
NULL
pointer problems! -
Leaving a large portion of the process address space starting at
0x0
empty allows the kernel to catch these errors, including offsets againstNULL
caused byNULL
structures:
-