basic dynamic memory allocation Flashcards
which area of memory does malloc manage
heap
explicit vs implicit allocator
explicit: you do it. malloc and free
implicit: im nice :) garbage collection
throughput
number of completed requests per unit of time
5000 mallocs and 5000 frees in 10 seconds = 1k operations/sec & mentally insane
peak memory utilization
basically the most payload you’ve had thus far. you just care abt the peak. so if you free’d, no nah you didnt.
max payload over lifetime / heap size
so get your mallocs in
fragmentation ruins this :(
internal fragmentation
payload / block size
caused by the need for headers & padding , or design implementation
external fragmentation
holes left in the heap from a mix of mallocs and free
sometimes you have enough memory for a malloc, but they’re not together
methods of tracking free blocks
- implicit list - all blocks in one list (linear allocation time)
- explicit list - all free blocks in one list (linear with respect to free blocks)
- segregated free list (hw3) - different free list for different sizes (approximate a best fit)
- free blocks sorted by size perfectly, such as with a red-black tree
First fit policy
start from beginning of list, iterate until ones found
causes splinters
Next fit
starting from where you last were, search until ones found
worse fragmentation
best fit policy
do an exhaustive search of list and find the best one
slower, but best memory usage
splitting
cutting a free block into two blocks so you only use whats absolutely needed
coalescing
when freeing a block, merge it with any potential neighboring free blocks to avoid external fragmentation.
in order to have bidirectional coalescing you need a header and a footer
immediate coalescing
coalesce when you free.
deferred coalescing
coalesce when you malloc and search a list.
another option is to coalesce when your external fragmentation gets too high
LIFO insertion policy
put free block at beginning of list