Memory Management Flashcards
mono-programming
no memory abstraction
Naive approach: Move each program to a dedicated region
Relocation problem
base register
operates dynamic allocation
limit register
enforces protection
swapping issue
may lead to memory fragmentation → Memory compaction → empty chunks to small to be used
problem: allowing extra room for process growth
issue: how much room?
memory usage vs. OOM risk
What to do on OOMs?
kill process
relocate process
swap out
bitmap memory allocation
finding holes requires slow scanning
slow in finding holes of a certain length
list memory allocation
slow allocation, slow deallocation
holes sorted by address for fast coalescing
linked list memory management
- In practice, a doubly linked is often used ⇒ makes freeing easy
- Can easily check if previous free
- Can easily adjust the pointers
first fit
take first fitting hole
next fit
take next fitting hole
slower than first fit in practice
best fit
take best fitting hole
prone to fragmentation
worst fit
take worst fitting hole -> maximise remains
poor performance in practice
quick fit
keep holes of different size
poor coalescing performance
buddy allocation scheme
improves quick fit’s coalescing performance
buddy allocator
Fast and limits external fragmentation but considerable internal fragmentation
Often combined with other allocators
So slab allocator gets memory from buddy and parcels this out to callers of kmalloc()
slab allocator
- Allocation + freeing of objects of the same type: very common in OS
- Speed is of the essence
- Unfortunately, allocating, initializing, and destroying objects all take timeE.g., list of free chunks → allocation requires traversing list to find a chunk of sufficient size
- Goal:
- make metadata for allocator (e.g., to track what memory is free) very fast
- cache as much as possible
SLABs
- slab allocator supports memory allocation for kernel
- kernel needs many different temporary objects: e.g. dentry
- temporary kernel objects
- can be both small and large
- are often allocated and often freed (efficiency is crucial)
- the buddy allocator, which operates with areas composed of entire frames of memory, is not suitable for this
paging
- Divide physical and virtual memory into pages of fixed size (e.g. 4096 bytes)
- Translate virtual pages into physical pages (frames)
total physical memory size
page frame * number of frames
32 bit systems
- 32 bit address space and 32 page table entries (PTEs)
- 20 bits for address + 12 to encode other things
64-bit systems
- really: 48-bit or 57-bit address space and 64-bit PTEs
- 2^48 bytes
two level page table (32 bit)
20 bits ⇒ split into two 10 bit parts: the first 10 bits are used as an index to the top-level page table; the second 10 bits are used as an index to the second-level page table
four level page table
page global directory → page upper directory → page midlevel directory → pte → page
each pte points to the base of the next level → 9 bits give an offset
5 memory accesses needed for each memory access: each table address and the page itself
inverted page tables
Translation Lookaside Buffer
- Problem: MMU has to translate every single memory access → Performance problem
- Solution: Cache previous translations(virtual to physical), hoping programs exhibit a high degree of locality.