CS 61C L06 C Memory Management (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia www.cs.berkeley.edu/~ddgarcia inst.eecs.berkeley.edu/~cs61c CS61C.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Advertisements

Dynamic Memory Management
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
CPSC 388 – Compiler Design and Construction
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
CS 61C L07 More Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS61C L05 C Structures, Memory Management (1) Garcia, Spring 2005 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
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 L34 Virtual Memory I (1) Garcia, Spring 2007 © UCB 3D chips from IBM  IBM claims to have found a way to build 3D chips by stacking them together.
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
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:
Intro to Computer Architecture
Run time vs. Compile time
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.
C and Data Structures Baojian Hua
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.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 34 – Virtual Memory II Researchers at Stanford have developed “nanoscale.
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.
COP4020 Programming Languages
CS61C L05 C Memory Management (1) Pearce, Summer 2010 © UCB inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 – C Memory Management
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.
ICS 145B -- L. Bic1 Project: Main Memory Management Textbook: pages ICS 145B L. Bic.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
PA0 due 60 hours. Lecture 4 Memory Management OSTEP Virtualization CPU: illusion of private CPU RAM: illusion of private memory Concurrency Persistence.
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.
Cs641 pointers/numbers.1 Overview °Stack and the Heap °malloc() and free() °Pointers °numbers in binary.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
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.
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
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.
Chapter 17 Free-Space Management
Storage Allocation Mechanisms
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
CSC 253 Lecture 8.
Optimizing Malloc and Free
CS Introduction to Operating Systems
CSC 253 Lecture 8.
Lecturer SOE Dan Garcia
Memory Allocation CS 217.
CS61C - Machine Structures Lecture 4 C Structures Memory Management
Memory Management Overview
Topic 3-b Run-Time Environment
There is one handout today at the front and back of the room!
Lecturer PSOE Dan Garcia
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Presentation transcript:

CS 61C L06 C Memory Management (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 6 – C Memory Management This one was   a SUPER bowl! What a game! One of the best in history, easily.

CS 61C L06 C Memory Management (2) Garcia, Spring 2004 © UCB Clarifications to Friday’s lecture char *strcpy(char *dst, char *src); copy the contents of string src to the memory at dst and return dst. The caller must ensure that dst has enough memory to hold the data to be copied. Thanks to Sal for pointing this out!

CS 61C L06 C Memory Management (3) Garcia, Spring 2004 © UCB 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 and freed when procedure returns. -NB: main() is a procedure int myGlobal; main() { int myTemp; }

CS 61C L06 C Memory Management (4) Garcia, Spring 2004 © UCB The Stack Stack frame includes: Return address Parameters Space for other local variables Stack frames contiguous blocks of memory; stack pointer tells where top stack frame is When procedure ends, stack frame is tossed off the stack; frees memory for future stack frames frame SP

CS 61C L06 C Memory Management (5) Garcia, Spring 2004 © UCB Stack Last In, First Out (LIFO) memory usage 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

CS 61C L06 C Memory Management (6) Garcia, Spring 2004 © UCB Pointers in C allow access to deallocated memory, leading to hard-to-find bugs ! int * ptr () { int y; y = 3; return &y; }; main () { int *stackAddr,content; stackAddr = ptr(); content = *stackAddr; printf("%d", content); /* 3 */ content = *stackAddr; printf("%d", content); /* */ }; Who cares about stack management? main ptr() (y==3) SP main SP main printf() (y==?) SP

CS 61C L06 C Memory Management (7) Garcia, Spring 2004 © 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 storage): data lives until deallocated by programmer C requires knowing where objects are in memory, otherwise don't work as expect Java hides location of objects

CS 61C L06 C Memory Management (8) Garcia, Spring 2004 © 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(4); /* malloc returns type (void *), so need to cast to right type */ malloc() : Allocates raw, uninitialized memory from heap

CS 61C L06 C Memory Management (9) Garcia, Spring 2004 © UCB Review: 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

CS 61C L06 C Memory Management (10) Garcia, Spring 2004 © 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

CS 61C L06 C Memory Management (11) Garcia, Spring 2004 © 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

CS 61C L06 C Memory Management (12) Garcia, Spring 2004 © 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.

CS 61C L06 C Memory Management (13) Garcia, Spring 2004 © 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 R2 (1 byte) R1 (100 bytes)

CS 61C L06 C Memory Management (14) Garcia, Spring 2004 © 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 R2 (1 byte) R3?

CS 61C L06 C Memory Management (15) Garcia, Spring 2004 © 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 linked list, the pointer field is unused in an allocated block

CS 61C L06 C Memory Management (16) Garcia, Spring 2004 © 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. 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

CS 61C L06 C Memory Management (17) Garcia, Spring 2004 © 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

CS 61C L06 C Memory Management (18) Garcia, Spring 2004 © UCB Administrivia Read Hilfinger’s notes 10.1 to 10.4 and P&H Sec by Wednesday Lab 3 on abstraction, pointers, memory this Thursday HW 2 due 23:59 today… HW 3 next Monday (up soon)

CS 61C L06 C Memory Management (19) Garcia, Spring 2004 © UCB Peer Instruction – Pros and Cons of fits A. The con of first-fit is that it results in many small blocks at the beginning of the free list B. The con of next-fit is it is slower than first-fit, since it takes longer in steady state to find a match C. The con of best-fit is that it leaves lots of tiny blocks ABC 1: FFF 2: FFT 3: FTF 4: FTT 5: TFF 6: TFT 7: TTF 8: TTT

CS 61C L06 C Memory Management (20) Garcia, Spring 2004 © UCB What’s this CS&E stuff good for? Only Sociology majors help real people? Computer technology (CS&E majors) offers extraordinary aid to the disabled Bionics : Sensors in latex fingers instantly register hot and cold, and an electronic interface in his artificial limb stimulates the nerve endings in his upper arm, which then pass the information to his brain. The $3,000 system allows his hand to feel pressure and weight, so for the first time since losing his arms in a 1986 accident, he can pick up a can of soda without crushing it or having it slip through his fingers. One Digital Day

CS 61C L06 C Memory Management (21) Garcia, Spring 2004 © 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.

CS 61C L06 C Memory Management (22) Garcia, Spring 2004 © 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)