Download Understanding C Memory Allocation and Heap Management at UC Santa Barbara and more Assignments Mechanical Engineering in PDF only on Docsity!
ECE 15B COMPUTER ORGANIZATION
Dr. Rahul Singh
Lecture 14
Memory Management I
May 19, 2009 Lecture 14: Memory Management Electrical and Computer Engineering University of California, Santa Barbara
Announcements
⢠HW
⢠Project
⢠Project
- Assigned today
- Due June 6 at 5:00 PM
⢠Quiz
- In class, next Thursday (May 21)
- Covers up until last lecture (#13) Lecture 14: Memory Management I May 19, 2009 Electrical and Computer Engineering University of California, Santa Barbara
Where allocated?
⢠Structure declaration does not allocate
memory
⢠Variable declaration does allocate memory
⢠If declare outside a procedure
- allocated in static storage
⢠If declare inside procedure
- allocated on the stack & freed when procedure returns Lecture 14: Memory Management I May 19, 2009
int myGlobal;
main() { int temp; }
Electrical and Computer Engineering University of California, Santa Barbara
C Memory Management
⢠C has 3 pools of memory
⢠Static storage
- global variable storage, basically permanent, entire program run
⢠The Stack
- local variable storage, parameters, return address ("stack frame" in C)
⢠The Heap (dynamic storage)
- data lives until deallocated by programmer
⢠C requires knowing where objects are in
memory, otherwise won't work as expect
Lecture 14: Memory Management I May 19, 2009
Electrical and Computer Engineering University of California, Santa Barbara
Normal C Memory Management
- A programʼs address space
contains 4 regions:
- stack
- local variables
- grows downward
- heap
- space requested for pointers via malloc()
- resizes dynamically, grows upward
- static data
- variables declared outside main
- does not grow or shrink
- code
- loaded when program starts
- does not change Lecture 14: Memory Management I May 19, 2009 For now, OS somehow prevents accesses between stack and heap (gray hash lines). Wait for virtual memory ~ FFFF FFFFhex ~ 0hex stack heap static data
code
Electrical and Computer Engineering University of California, Santa Barbara
Intel 80x86 C Memory Management
- A C programʼs 80x86 address space :
- heap
- space requested for pointers via malloc()
- resizes dynamically, grows upward
- static data
- variables declared outside main
- does not grow or shrink
- code
- loaded when program starts
- does not change
- stack
- local variables
- grows downward Lecture 14: Memory Management I May 19, 2009 ~ 08000000hex stack heap static data
code
Electrical and Computer Engineering University of California, Santa Barbara The Heap (Dynamic memory)
- Large pool of memory
- Not allocated in contiguous order
- back-to-back requests for heap memory could result blocks very far apart
- where C++/Java new command allocates memory
- In C, specify number of bytes of memory explicitly
to allocate item
int *ptr; ptr = (int ) malloc(4); / malloc returns type (void *), so need to cast to right type */
- malloc(): Allocates raw, uninitialized memory from heap Lecture 14: Memory Management I May 19, 2009 Electrical and Computer Engineering University of California, Santa Barbara Dynamic Memory Allocation
- C has operator sizeof()
- gives size in bytes (of type or variable)
- Assume size of objects can be misleading &
is bad style, so use sizeof(type)
- Many years ago an int was 16 bits, and
programs assumed it was 2 bytes
Lecture 14: Memory Management I May 19, 2009
Electrical and Computer Engineering University of California, Santa Barbara
- An example: Heap Management Lecture 14: Memory Management I May 19, 2009 R1 (100 Bytes) Request R for 100 Bytes Request R for 1 Bytes R1 (100 Bytes) R2 (10 Bytes) Memory from R1 is freed R2 (10 Bytes) Request R for 50 Bytes R3 (50 Bytes)? R2 (10 Bytes) R3 (50 Bytes)? Electrical and Computer Engineering University of California, Santa Barbara Linked List Malloc/Free Implementation
- Each block of memory is preceded by a
header that has two fields:
- size of the block
- a pointer to the next block
- All free blocks are kept in a linked list
- the pointer field is unused in an allocated block Lecture 14: Memory Management I May 19, 2009 Electrical and Computer Engineering University of California, Santa Barbara Linked List Malloc/Free Implementation
- malloc() searches the free list for a block
that is big enough.
- If none is found, more memory is requested from
the operating system.
- free() checks if the blocks adjacent to the
freed block are also free
- If so, adjacent free blocks are merged
(coalesced) into a single, larger free block
- Otherwise, the freed block is just added to the
free list
Lecture 14: Memory Management I May 19, 2009 Electrical and Computer Engineering University of California, Santa Barbara Choosing a block in malloc()
- If there are multiple free blocks of memory
that are big enough for some request:
How do we choose which one to use?
- best-fit
- choose the smallest block that is big enough for the request
- first-fit
- choose the first block we see that is big enough
- next-fit
- like first-fit but remember where we finished searching and resume searching from there Lecture 14: Memory Management I May 19, 2009
Electrical and Computer Engineering University of California, Santa Barbara Tradeoffs of allocation policies
- Best-fit
- Tries to limit fragmentation but at the cost of time (must examine all free blocks for each malloc).
- Leaves lots of small blocks ( why? )
- First-fit:
- Quicker than best-fit ( why? )
- But potentially more fragmentation.
- Tends to concentrate small blocks at the beginning of the free list ( why? )
- Next-fit
- Does not concentrate small blocks at front like first-fit
- should be faster as a result. Lecture 14: Memory Management I May 19, 2009 Electrical and Computer Engineering University of California, Santa Barbara And in conclusionā¦
- C has 3 pools of memory
- Static storage
- global variable storage, basically permanent, entire program run
- The Stack
- local variable storage, parameters, return address
- The Heap (dynamic storage)
- malloc() grabs space from here,
- free() returns it Lecture 14: Memory Management I May 19, 2009 Electrical and Computer Engineering University of California, Santa Barbara Conlcusion
- malloc() handles free space with freelist
- Three different ways to find free space when
given a request
- First fit
- find first one thatʼs free
- Next fit
- same as first, but remembers where left off
- Best fit
- finds most āsnugā free space Lecture 14: Memory Management I May 19, 2009