Professional Documents
Culture Documents
Practical Programming in C-LECTURE 11 - 6-087-January-Iap-2010 MIT6 - 087IAP10 - Lec11
Practical Programming in C-LECTURE 11 - 6-087-January-Iap-2010 MIT6 - 087IAP10 - Lec11
Review
Dynamic Memory Allocation
Designing the malloc() Function
A Simple Implementation of malloc()
A Real-World Implementation of malloc()
Using malloc()
Using valgrind
Garbage Collection
memset()
macros
Review
Dynamic Memory Allocation
Designing the malloc() Function
A Simple Implementation of malloc()
A Real-World Implementation of malloc()
Using malloc()
Using valgrind
Garbage Collection
<sys/mman.h>)
Virtual memory can be returned to OS using munmap()
Virtual memory either backed by a le/device or by
demand-zero memory:
Mapping memory
Mapping memory:
void mmap( void s t a r t , s i z e _ t l e n g t h , i n t p r o t ,
i n t f l a g s , i n t fd , o f f _ t o f f s e t ) ;
asks OS to map virtual memory of specied length, using
usually NULL
Unmap memory:
int munmap(void start, size_t length);
The heap
grows
To resize heap, can use sbrk() function:
void sbrk(int inc ); / returns old value of brk_ptr /
Requirements
Fragmentation
free blocks
Design choices
Tracking blocks
different sizes
Explicit lists stored within blocks (pointers in payload
10
Block structures
11
Block structures
12
Positioning allocations
relatively fast
Best t can have best memory utilization, but at cost of
13
complicated to implement
Similarly, can join adjacent free blocks during (or after)
adjacent blocks
Footer with pointer to head of block enable successive
14
15
Initialization
16
Allocating data
17
Freeing data
18
order)
When freeing, add free block to end of list; set pointer to
19
found at
http://g.oswego.edu/dl/html/malloc.html
Chunks implemented as in segregated free list, with
spaced logarithmically
Placement done in best-t order
Deferred coalescing and splitting performed to minimize
overhead
20
Review
Dynamic Memory Allocation
Designing the malloc() Function
A Simple Implementation of malloc()
A Real-World Implementation of malloc()
Using malloc()
Using valgrind
Garbage Collection
21
Using malloc()
much as possible
Growing memory using realloc() can reduce
fragmentation
Repeated allocation and freeing of variables can lead to
21
~donahoo/tools/valgrind/
valgrind program provides several performance tools,
including memcheck:
1
--leak-check=yes program.o
memory leaks
Does not trigger on out-of-bounds index errors for arrays
on the stack
Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
22
23
Review
Dynamic Memory Allocation
Designing the malloc() Function
A Simple Implementation of malloc()
A Real-World Implementation of malloc()
Using malloc()
Using valgrind
Garbage Collection
24
Garbage collection
terminates
Other languages like Java implement garbage collectors to
24
Garbage collection
terminates
Other languages like Java implement garbage collectors to
24
above a threshold
25
goes
Sweep: traverse entire heap, freeing unmarked data
Requires two complete traversals of memory, takes a lot of
time
Implementation available at http:
//www.hpl.hp.com/personal/Hans_Boehm/gc/
26
27
28
duplicate heap
After copying phase, entire old heap (the from-space) is
freed
Code can only use half the heap
29
to-space
Starting at beginning of to-space, iterate through memory,
30
Summary
Topics covered:
Dynamic memory allocation
the heap
designing a memory allocator
a real world allocator
Using malloc()
Using valgrind
Garbage collection
mark-and-sweep collector
copying collector
31
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.