Memory Management in C.pdf
(
415 KB
)
Pobierz
Advanced C Programming
Memory Management II
(malloc, free, alloca, obstacks, garbage collection)
Sebastian Hack
hack@cs.uni-sb.de
Christoph Weidenbach
weidenbach@mpi-inf.mpg.de
16.12.2008
saarland
university
computer science
1
Contents
Memory Allocation
alloca
/ Variable length arrays
malloc
and
free
Memory Allocation in UNIX
The Doug Lea Allocator
Binning
allocate
free
Chunk Coalescing
Region-based memory management
Obstacks
Garbage Collection in C
A Critique of Custom Memory Allocation
Bibliography
2
Problems of Memory Allocation
Fragmentation
Not being able to reuse free memory
Free memory is split up in many small pieces
Cannot reuse them for large-piece requests
Primary objective of today’s allocators is to avoid fragmentation
Locality
Temporal and spacial locality go along with each other
Memory accesses near in time are also near in space
Try to serve timely near requests with memory in the same region
Less paging
Memory allocation locality not that important for associative caches
Enabling locality by the programmer more important
3
Practical Considerations (see [Lea])
A good memory allocator needs to balance a number of goals:
Minimizing Space
The allocator should not waste space
Obtain as little memory from the system as possible
Minimize fragmentation
Minimizing Time
malloc, free
and
realloc
should be as fast as
possible in the average case
Maximizing Tunability
Configure optional features
(statistics info, debugging, . . . )
Maximizing Locality
Allocate chunks of memory that are typically used
together near each other
Helps minimize page and cache misses during
program execution
Minimizing Anomalies
Perform well across wide range of real loads
4
Approaches
Allocate and Free
Allocating and freeing done by the programmer
Bug-prone: Can access memory after being freed
Potentially efficient: Programmer should know when to free what
Garbage Collection
User allocates
System
automatically
frees dead chunks
Less bug-prone
Potentially inefficient:
Overhead of the collection, many dead chunks
Region-based approaches
User allocates chunks inside a region
Only the region can be freed
Efficiency of allocate and free
Slightly less bug-prone
many dead chunks
5
Plik z chomika:
musli_com
Inne pliki z tego folderu:
A Book on C_ Programming in C (4th ed.) [Kelley & Pohl 1998-01-08].pdf
(48528 KB)
Advanced C and C Compiling [Stevanovic 2014-04-28].pdf
(29765 KB)
Embedded C Programming and the Atmel AVR (2nd ed.) [Barnett, Cox & O'Cull 2006-06-05].pdf
(82508 KB)
Beginning C for Arduino_ Learn C Programming for the Arduino and Compatible Microcontrollers [Purdum 2012-12-03].pdf
(28335 KB)
21st Century C [Klemens 2012-11-05].pdf
(7370 KB)
Inne foldery tego chomika:
3D Design - Programming
ActionScript
Actionscript - Flash - Flex - Air
Ada
ADO
Zgłoś jeśli
naruszono regulamin