CS61C L05 C Memory Management (1) Pearce, Summer 2010 © UCB inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 – C Memory Management 2010-06-28.

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
1 Optimizing Malloc and Free Professor Jennifer Rexford COS 217 Reading: Section 8.7 in K&R book
CPSC 388 – Compiler Design and Construction
Lab 3: Malloc Lab. “What do we need to do?”  Due 11/26  One more assignment after this one  Partnering  Non-Honors students may work with one other.
CS 61C L07 More Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
1 CSE 380 Computer Operating Systems Instructor: Insup Lee University of Pennsylvania, Fall 2002 Lecture Note: Memory Management.
CS 61C L06 Memory Management (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #6: Memory Management.
CS61C L4 C Memory Management (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
CS 536 Spring Run-time organization Lecture 19.
Memory Management Memory Areas and their use Memory Manager Tasks:
1 Optimizing Malloc and Free Professor Jennifer Rexford
CS 61C L06 C Memory Management (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Memory Management A memory manager should take care of allocating memory when needed by programs release memory that is no longer used to the heap. Memory.
CS61C L06 C Memory Management (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C :
Memory Management Chapter 5.
CS61C L06 C Memory Management (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C :
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
CS61CL L02 Dynamic Storage (1) Huddleston, Summer 2009 © UCB Jeremy Huddleston inst.eecs.berkeley.edu/~cs61c CS61CL : Machine Structures Lecture #3 -
CS61C L06 C Memory Management (1) Garcia, Spring 2005 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS61C L06 C Memory Management (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
1 Inner Workings of Malloc and Free Professor Jennifer Rexford COS 217.
CS61C L5 Memory Management; Intro MIPS (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures.
CS61C Midterm Review on C & Memory Management Fall 2006 Aaron Staley Some material taken from slides by: Michael Le Navtej Sadhal.
CS 61C L06 C Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C :
CS 61C L04 C Structures, Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L07 More Memory Management (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Memory Allocation CS Introduction to Operating Systems.
The memory allocation problem Define the memory allocation problem Memory organization and memory allocation schemes.
Real-Time Concepts for Embedded Systems Author: Qing Li with Caroline Yao ISBN: CMPBooks.
COP4020 Programming Languages
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
CS 61C L2.1.2 MM and MIPS (1) K. Meinz, Summer 2004 © UCB CS61C : Machine Structures Lecture Garbage Collection & Intro to MIPS Kurt Meinz.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
Heap Management. What is really stored on the heap? Housekeeping Users Data Buffer Next Block Data Housekeeping 0x7000 0x7008 int main() { int *x,*y;
Stack and Heap Memory Stack resident variables include:
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
CS 61C L2.1.1 Memory Management (1) K. Meinz, Summer 2004 © UCB CS61C : Machine Structures Lecture Memory Management Kurt Meinz inst.eecs.berkeley.edu/~cs61c.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
Chapter 17 Free-Space Management Chien-Chung Shen CIS, UD
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Cs641 pointers/numbers.1 Overview °Stack and the Heap °malloc() and free() °Pointers °numbers in binary.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Memory Management Overview.
CS61C L5 Memory Management; Intro MIPS (1) Garcia, Fall 2005 © UCB Lecturer PSOE, new dad Dan Garcia inst.eecs.berkeley.edu/~cs61c.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Consider 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)
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
CS 241 Discussion Section (12/1/2011). Tradeoffs When do you: – Expand Increase total memory usage – Split Make smaller chunks (avoid internal fragmentation)
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
CS61C L07 More Memory Management (1) Garcia, Spring 2010 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS61C L05 C Memory Management (1) Chae, Summer 2008 © UCB Albert Chae Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 – C.
Memory Management Chapter 5 Advanced Operating System.
Chapter 17 Free-Space Management
Memory Management Memory Areas and their use Memory Manager Tasks:
CS 326 Programming Languages, Concepts and Implementation
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Run-time organization
Memory Management Memory Areas and their use Memory Manager Tasks:
The Hardware/Software Interface CSE351 Winter 2013
Optimizing Malloc and Free
CS Introduction to Operating Systems
Lecturer SOE Dan Garcia
Memory Management Overview
There is one handout today at the front and back of the room!
Lecturer PSOE Dan Garcia
Memory Management Memory Areas and their use Memory Manager Tasks:
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Presentation transcript:

CS61C L05 C Memory Management (1) Pearce, Summer 2010 © UCB inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 – C Memory Management Symmetric multiprocessor MIPS support for Android  MIPS Technologies (founded by John Hennessy, author of your textbook!) recently announced support for SMP on Android. This week, you’ll learn MIPS! Instructor Paul Pearce

CS61C L05 C Memory Management (2) Pearce, Summer 2010 © UCB Review Use handles to change pointers Create abstractions (and your own data structures) with structures Dynamically allocated heap memory must be manually deallocated in C. Use malloc() and free() to allocate and de-allocate persistent storage.

CS61C L05 C Memory Management (3) Pearce, Summer 2010 © UCB How big are structs? Recall C operator sizeof() which gives size in bytes (of type or variable) How big is sizeof(p) ? struct p { char x; int y; }; 5 bytes? 8 bytes? Compiler may word align integer y Takeaway: Structs can be padded, use sizeof()

CS61C L05 C Memory Management (4) Pearce, Summer 2010 © UCB Don’t forget the globals! Remember: Structure declaration does not allocate memory Variable declaration does allocate memory So far we have talked about several different ways to allocate memory for data: 1.Declaration of a local variable int i; struct Node list; char *string; int ar[n]; 2.“Dynamic” allocation at runtime by calling allocation function (malloc). ptr = (struct Node *) malloc(sizeof(struct Node)*n); One more possibility exists… 3.Data declared outside of any procedure (i.e., before main). Similar to #1 above, but has “global” scope. int myGlobal; main() { }

CS61C L05 C Memory Management (5) Pearce, Summer 2010 © UCB 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 (location of “activation records” in Java or “stack frame” in C) The Heap (dynamic malloc storage): data lives until deallocated by programmer C requires knowing where objects are in memory, otherwise things don’t work as expected Java hides location of objects

CS61C L05 C Memory Management (6) Pearce, Summer 2010 © UCB 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 code static data heap stack For now, OS somehow prevents accesses between stack and heap (gray hash lines). Wait for virtual memory ~ FFFF FFFF hex ~ 0 hex

CS61C L05 C Memory Management (7) Pearce, Summer 2010 © UCB Where are variables allocated? If declared outside a procedure, allocated in “static” storage If declared inside procedure, allocated on the “stack” and freed when procedure returns. main() is a procedure int myGlobal; main() { int myTemp; }

CS61C L05 C Memory Management (8) Pearce, Summer 2010 © UCB The Stack Stack frame includes: Return “instruction” address Parameters Space for other local variables Stack frames contiguous blocks of memory; stack pointer tells where bottom stack frame is When procedure ends, stack frame is tossed off the stack; frees memory for future stack frames frame SP

CS61C L05 C Memory Management (9) Pearce, Summer 2010 © UCB Stack Last In, First Out (LIFO) data structure main () { a(0); } void a (int m) { b(1); } void b (int n) { c(2); } void c (int o) { d(3); } void d (int p) { } stack Stack Pointer Stack grows down

CS61C L05 C Memory Management (10) Pearce, Summer 2010 © UCB Pointers in C allow access to deallocated memory, leading to hard-to-find bugs ! int *fun() { int y; y = 3; return &y; }; main () { int *stackAddr,content; stackAddr = fun(); content = *stackAddr; printf("%d", content); /* 3 */ content = *stackAddr; printf("%d", content); /* */ }; Who cares about stack management? main fun() (y==3) SP main SP main printf() (y==?) SP

CS61C L05 C Memory Management (11) Pearce, Summer 2010 © UCB 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 Java new command allocates memory In C, specify number of bytes of memory explicitly to allocate item int *ptr; ptr = (int *) malloc(sizeof(int)); /* malloc returns type (void *), so need to cast to right type */ malloc() : Allocates raw, uninitialized memory from heap

CS61C L05 C Memory Management (12) Pearce, Summer 2010 © UCB Memory Management How do we manage memory? Code, Static storage are easy: they never grow or shrink Stack space is also easy: stack frames are created and destroyed in last-in, first-out (LIFO) order Managing the heap is tricky: memory can be allocated / deallocated at any time

CS61C L05 C Memory Management (13) Pearce, Summer 2010 © UCB Heap Management Requirements Want malloc() and free() to run quickly. Want minimal memory overhead Want to avoid fragmentation* – when most of our free memory is in many small chunks In this case, we might have many free bytes but not be able to satisfy a large request since the free bytes are not contiguous in memory. * This is technically called external fragmention

CS61C L05 C Memory Management (14) Pearce, Summer 2010 © UCB Heap Management An example Request R1 for 100 bytes Request R2 for 1 byte Memory from R1 is freed Request R3 for 50 bytes What if R3 was a request for 101 bytes? R2 (1 byte) R1 (100 bytes)

CS61C L05 C Memory Management (15) Pearce, Summer 2010 © UCB Heap Management R2 (1 byte)R3? An example Request R1 for 100 bytes Request R2 for 1 byte Memory from R1 is freed Request R3 for 50 bytes What if R3 was a request for 101 bytes?

CS61C L05 C Memory Management (16) Pearce, Summer 2010 © UCB Administrivia You made it through the first week! Yay! Don’t forget about the newsgroup! Hasn’t been used as much as Paul would like. Paul has elaborated on some things from lecture on the newsgroup! We start assembly tomorrow. Don’t forget about the reading HW 2 due tomorrow at midnight. Project 1 out now. Start early, lots of code to understand (and it’s really fun!) Check back of handout for CS Illustrated on pointers and arrays!

CS61C L05 C Memory Management (17) Pearce, Summer 2010 © UCB More Administrivia! Tentative schedule for the rest of the summer now online! Plan accordingly! Midterm currently slated for Thursday July 15 th from 6pm-9pm Does anyone have a hard conflict with this time? Final time will be determined based on how the midterm goes at the scheduled time. The final will be on Thursday, August 12 th Pick up a copy of the cheating policy from the front. On it please write your name, login, and then sign it. You’ll need to give this to your Lab TA in order to be checked off for Lab 3!

CS61C L05 C Memory Management (18) Pearce, Summer 2010 © UCB K&R Malloc/Free Implementation From Section 8.7 of K&R Code in the book uses some C language features we haven’t discussed and is written in a very terse style, don’t worry if you can’t decipher the code Each block of memory is preceded by a header that has two fields: size of the block and a pointer to the next block All free blocks are kept in a circular linked list, the pointer field is unused in an allocated block

CS61C L05 C Memory Management (19) Pearce, Summer 2010 © UCB K&R 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. If what it gets can’t satisfy the request, it fails. 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

CS61C L05 C Memory Management (20) Pearce, Summer 2010 © UCB 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

CS61C L05 C Memory Management (21) Pearce, Summer 2010 © UCB Slab Allocator A different approach to memory management (used in GNU libc ) Divide blocks in to “large” and “small” by picking an arbitrary threshold size. Blocks larger than this threshold are managed with a freelist (as before). For small blocks, allocate blocks in sizes that are powers of 2 e.g., if program wants to allocate 20 bytes, actually give it 32 bytes

CS61C L05 C Memory Management (22) Pearce, Summer 2010 © UCB Slab Allocator Bookkeeping for small blocks is relatively easy: just use a bitmap for each range of blocks of the same size Allocating is easy and fast: compute the size of the block to allocate and find a free bit in the corresponding bitmap. Freeing is also easy and fast: figure out which slab the address belongs to and clear the corresponding bit.

CS61C L05 C Memory Management (23) Pearce, Summer 2010 © UCB Slab Allocator 16 byte blocks: 32 byte blocks: 64 byte blocks: 16 byte block bitmap: byte block bitmap: byte block bitmap: 00

CS61C L05 C Memory Management (24) Pearce, Summer 2010 © UCB Slab Allocator Tradeoffs Extremely fast for small blocks. Slower for large blocks But presumably the program will take more time to do something with a large block so the overhead is not as critical. Minimal space overhead No fragmentation (as we defined it before) for small blocks, but still have wasted space!

CS61C L05 C Memory Management (25) Pearce, Summer 2010 © UCB Internal vs. External Fragmentation With the slab allocator, difference between requested size and next power of 2 is wasted e.g., if program wants to allocate 20 bytes and we give it a 32 byte block, 12 bytes are unused. We also refer to this as fragmentation, but call it internal fragmentation since the wasted space is actually within an allocated block. External fragmentation: wasted space between allocated blocks.

CS61C L05 C Memory Management (26) Pearce, Summer 2010 © UCB Buddy System Yet another memory management technique (used in Linux kernel) Like GNU’s “slab allocator”, but only allocate blocks in sizes that are powers of 2 (internal fragmentation is possible) Keep separate free lists for each size e.g., separate free lists for 16 byte, 32 byte, 64 byte blocks, etc.

CS61C L05 C Memory Management (27) Pearce, Summer 2010 © UCB Buddy System If no free block of size n is available, find a block of size 2n and split it in to two blocks of size n When a block of size n is freed, if its neighbor of size n is also free, combine the blocks in to a single block of size 2n Buddy is block in other half larger block Same speed advantages as slab allocator buddiesNOT buddies

CS61C L05 C Memory Management (28) Pearce, Summer 2010 © UCB Allocation Schemes So which memory management scheme (K&R, slab, buddy) is best? There is no single best approach for every application. Different applications have different allocation / deallocation patterns. A scheme that works well for one application may work poorly for another application.

CS61C L05 C Memory Management (29) Pearce, Summer 2010 © UCB Peer Instruction – Pros and Cons of fits 1)first-fit results in many small blocks at the beginning of the free list 2) next-fit is slower than first-fit, since it takes longer in steady state to find a match 3) best-fit leaves lots of tiny blocks 123 a) FFT b) FTT c) TFF d) TFT e) TTT

CS61C L05 C Memory Management (30) Pearce, Summer 2010 © UCB Peer Instruction – Pros and Cons of fits 1)first-fit results in many small blocks at the beginning of the free list 2) next-fit is slower than first-fit, since it takes longer in steady state to find a match 3) best-fit leaves lots of tiny blocks 123 a) FFT b) FTT c) TFF d) TFT e) TTT

CS61C L05 C Memory Management (31) Pearce, Summer 2010 © UCB 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. 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) Internal vs External fragmentation!

CS61C L05 C Memory Management (32) Pearce, Summer 2010 © UCB Bonus slides These are extra slides that used to be included in lecture notes, but have been moved to this, the “bonus” area to serve as a supplement. The slides will appear in the order they would have in the normal presentation

CS61C L05 C Memory Management (33) Pearce, Summer 2010 © UCB 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 code static data heap stack ~ hex

CS61C L05 C Memory Management (34) Pearce, Summer 2010 © UCB 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.