consider starting with 160 k of memory do: starting with 160 k of memory do: allocate p1 (50 k)...
TRANSCRIPT
ConsiderConsider
Starting with 160 k of memory do:Starting with 160 k of memory do: Allocate p1 (50 k)Allocate p1 (50 k) Allocate p2 (30 k)Allocate p2 (30 k) Allocate p3 (40 k)Allocate p3 (40 k) Free p2Free p2 Allocate p4 (40 k)Allocate p4 (40 k) Free p3 Free p3 Allocate p5 (60 k)Allocate p5 (60 k) Free p1Free p1 Allocate p6 (30k)Allocate p6 (30k)
Memory Allocation Memory Allocation AlgorithmsAlgorithms
Design YOUR algorithm for allocation Design YOUR algorithm for allocation and deallocation of memoryand deallocation of memory
Memory ManagementMemory Management Dynamic (heap)Dynamic (heap) Significant issuesSignificant issues
Significant execution time (16%)Significant execution time (16%) Memory performance not uniformMemory performance not uniform Allocation policiesAllocation policies BugsBugs
Dereference problemsDereference problems Memory leaksMemory leaks
Memory Allocation Memory Allocation StrategiesStrategies
Explicit vs. Implicit Memory AllocatorExplicit vs. Implicit Memory Allocator
General purpose vs. custom allocatorGeneral purpose vs. custom allocator
Software vs. hardwareSoftware vs. hardware
Allocation ExamplesAllocation Examplesp1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(2)
Goals of Good Goals of Good malloc/free malloc/free
Good execution-time performance Good execution-time performance
Good space utilizationGood space utilization
Good locality propertiesGood locality properties
FragmentationFragmentation
Poor memory utilization --- Poor memory utilization --- fragmentationfragmentation Internal – overhead associated with Internal – overhead associated with
a block of memorya block of memory External – have enough blocks of External – have enough blocks of
memory for a request, but not memory for a request, but not contiguouscontiguous
Space in use
Internal fragmentation
External External FragmentationFragmentation
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(6)
External fragmentation depends on future requests;thus difficult to anticipate
Bidirectional Coalescing Bidirectional Coalescing
Boundary tagsBoundary tags [Knuth73] [Knuth73]
Replicate size/allocated word Replicate size/allocated word at bottom of free blocksat bottom of free blocks
Allows us to traverse the Allows us to traverse the “list” backwards, but “list” backwards, but requires extra spacerequires extra space
Important and general Important and general technique!technique!
Boundary Tags Boundary Tags
size
1 word
Format ofallocated andfree blocks
ApplicationMemory
(and padding?)
a = 1: allocated block a = 0: free block
size: total block size
Application memory(allocated blocks only)
a
size aBoundary tag (footer)
4 4 4 4 6 46 4
Header
Your turnYour turn
Using boundary tag data structure, Using boundary tag data structure, define algorithms for:define algorithms for:
AllocationAllocation FreeFree
Key Allocator PoliciesKey Allocator Policies Placement policy:Placement policy:
First fit, worst fit, best fit, etc.First fit, worst fit, best fit, etc. Trades off lower throughput for less Trades off lower throughput for less
fragmentationfragmentation Splitting policy:Splitting policy:
When do we go ahead and split free blocks?When do we go ahead and split free blocks? How much internal fragmentation are we willing How much internal fragmentation are we willing
to tolerate?to tolerate? Coalescing policy:Coalescing policy:
Immediate coalescing: coalesce adjacent blocks Immediate coalescing: coalesce adjacent blocks each time free is called each time free is called
Deferred coalescing: try to improve Deferred coalescing: try to improve performance of free by deferring coalescing performance of free by deferring coalescing until needed. e.g.,until needed. e.g.,
RefinementsRefinements
Separate listsSeparate lists Binary buddyBinary buddy Lea allocatorLea allocator Custom allocatorsCustom allocators
Lea AllocatorLea Allocator An approximate best-fit allocator with An approximate best-fit allocator with
different behavior based on object sizedifferent behavior based on object size Small Objects (<64 bytes) allocated by exact-Small Objects (<64 bytes) allocated by exact-
size quicklistssize quicklists Medium Objects (<128K) – Medium Objects (<128K) – coalesce coalesce quicklistsquicklists Large Objects – allocate and free by Large Objects – allocate and free by mmapmmap
Generally considered the best allocator Generally considered the best allocator known (as of 2000, anyway)known (as of 2000, anyway)
http://g.oswego.edu/dl/html/malloc.htmlhttp://g.oswego.edu/dl/html/malloc.html
Why programmers use Why programmers use Custom Allocators?Custom Allocators?
Improving runtime performanceImproving runtime performance Reducing memory consumptionReducing memory consumption Improving software engineering (?)Improving software engineering (?)
Alternative Memory Alternative Memory ManagementManagement
Region (arenas) Region (arenas) Reserve memory blocks for program Reserve memory blocks for program
“parts”“parts” Deallocate entire regions, not per Deallocate entire regions, not per
allocationallocation Garbage collectionGarbage collection
Programmer allocates but doesn’t freeProgrammer allocates but doesn’t free ““System” keeps track of memory System” keeps track of memory
“pointed to” locations, removes the rest“pointed to” locations, removes the rest JavaJava
Why Garbage Collect at All?Why Garbage Collect at All?
SafetySafety Memory leaksMemory leaks Continued use of freed pointersContinued use of freed pointers
SimplicitySimplicity CorrectnessCorrectness Programming easeProgramming ease
The Two-Phase AbstractionThe Two-Phase Abstraction
1. Detection1. Detection
2. Reclamation2. Reclamation
Liveness and Garbage Liveness and Garbage
There is a root set which is defined There is a root set which is defined as live.as live.
Anything reachable from a live Anything reachable from a live pointer is also livepointer is also live
Everything else is garbageEverything else is garbage
The Root SetThe Root Set
The Root SetThe Root Set Static global and module variablesStatic global and module variables Local VariablesLocal Variables Variables on any activation stack(s)Variables on any activation stack(s)
Everyone elseEveryone else Anything Reachable From a live valueAnything Reachable From a live value
Reference Counting Reference Counting
Each allocated chunk has reference Each allocated chunk has reference count that shows how many locations count that shows how many locations point (refer) to this one.point (refer) to this one.
Advantages ???Advantages ??? Disadvantages ???Disadvantages ???
Mark-Sweep CollectionMark-Sweep Collection
Starting from the root set traverse all Starting from the root set traverse all pointers via depth/breadth first pointers via depth/breadth first search.search.
Free everything that is not marked.Free everything that is not marked.
More Information/DetailMore Information/Detail
If you wish to know more:If you wish to know more:
https://www.mpi-inf.mpg.de/https://www.mpi-inf.mpg.de/departments/rg1/teaching/departments/rg1/teaching/advancedc-ws08/script/lecture09.pdfadvancedc-ws08/script/lecture09.pdf