Dynamic Memory Allocation
Dynamic Memory Allocation
Objectives
Be able to analyze a memory allocators
performance
Memory Allocation
Static size, static allocation
Global variables
Linker allocates final virtual addresses
Executable stores these allocated addresses
Local variables
Compiler directs stack allocation
Stack pointer offsets stored directly in the code
Allocation
In both cases the memory allocator provides an abstraction of
memory as a set of blocks
Doles out free memory blocks to application
0xFFFFFFFF
0xFFBEC000
%sp
0xFF3DC000
Shared Libraries
brk
Heap
Read/Write Data
0x00010000
0x00000000
Used by allocators to
request additional memory
from the OS
brk initially set to the end
of the data section
Calls to sbrk increment
brk by incr bytes (new
virtual memory pages are
demand-zeroed)
incr can be negative to
reduce the heap size
Constraints
Applications:
Can issue arbitrary sequence of malloc and free requests
Free requests must correspond to an allocated block
Allocators
Cant control number or size of allocated blocks
Must respond immediately to all allocation requests
i.e., cant reorder or buffer requests
Robust
Can check that free(p1) is on a valid allocated object p1
Can check that memory references are to allocated space
Implicit Memory
Deallocation
+ Programmers dont need to free data
explicitly, easy to use
+ Some implementations could achieve better
spatial locality and less fragmentation in the
hands of your average programmers
- Price to pay: depends on implementation
But HOW could a memory manager know when
to deallocate data without instruction from
programmer?
Garbage Collection
How does the memory manager know when
memory can be freed?
In general we cannot know what is going to be
used in the future since it depends on conditionals
But we can tell that certain blocks cannot be used
if there are no pointers to them