Storage.

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Chapter 6 Data Types
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 9 MEMORY MANAGEMENT.
SPLINT STATIC CHECKING TOOL Sripriya Subramanian 10/29/2002.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Garbage Collection Introduction and Overview Christian Schulte Excerpted from presentation by Christian Schulte Programming Systems Lab Universität des.
Memory Management Tom Roeder CS fa. Motivation Recall unmanaged code eg C: { double* A = malloc(sizeof(double)*M*N); for(int i = 0; i < M*N; i++)
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
 Scope and linkage  Storage classes  Automatic memory  Dynamic memory  Memory Model.
Informática II Prof. Dr. Gustavo Patiño MJ
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Mark and Sweep Algorithm Reference Counting Memory Related PitFalls
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
C and Data Structures Baojian Hua
Run-time Environment and Program Organization
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
Memory Layout C and Data Structures Baojian Hua
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
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.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Outline Midterm results Static variables Memory model
Compiler Construction
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
C++ Memory Overview 4 major memory segments Key differences from Java
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
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)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
CSE 220 – C Programming malloc, calloc, realloc.
Object Lifetime and Pointers
Garbage Collection What is garbage and how can we deal with it?
Overview 4 major memory segments Key differences from Java stack
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
ENEE150 Discussion 07 Section 0101 Adam Wang.
Dynamic Memory Allocation
Storage Management.
Concepts of programming languages
CSCI206 - Computer Organization & Programming
Overview 4 major memory segments Key differences from Java stack
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory.
Name Binding and Object Lifetimes
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Pointers, Dynamic Data, and Reference Types
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Run-time environments
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Storage

Where do programs get memory? Three types Static Automatic Dynamic

Static Memory Each corresponds to a single variable in the program Not all variables, only Globals In C, local variables declared with the static keyword Lifetime Created/initialized on program start Deallocated on program termination

Static Memory Advantages No runtime cost for memory management Disadvantages Fixed amount of memory can not grow or shrink

Static Memory int x = 42; int a[3] = {1, 2, 3}; char *msg = "Hello"; void f() { static int z = 10; printf("%s\n",z); z = 20; } int main() { f(); return 0;

Static Memory int x = 42; int a[3] = {1, 2, 3}; char *msg = "Hello"; void f() { static int z = 10; printf("%s\n",z); z = 20; } int main() { f(); return 0;

Static Memory int x = 42; int a[3] = {1, 2, 3}; char *msg = "Hello"; void f() { static int z = 10; printf("%s\n",z); z = 20; } int main() { f(); return 0;

Static Memory Output: 10 20 int x = 42; int a[3] = {1, 2, 3}; char *msg = "Hello"; void f() { static int z = 10; printf("%s\n",z); z = 20; } int main() { f(); return 0; Output: 10 20

Static Memory Output: 10 20 int x = 42; int a[3] = {1, 2, 3}; char *msg = "Hello"; void f() { static int z = 10; printf("%s\n",z); z = 20; } int main() { f(); return 0; Output: 10 20

Static Memory Output: 10 20 int x = 42; int a[3] = {1, 2, 3}; char *msg = "Hello"; void f() { static int z = 10; printf("%s\n",z); z = 20; } int main() { f(); return 0; Output: 10 20

Automatic Variables Local variables of functions Reside in the call stack Each call gets its own instance Even recursive calls Lifetime Allocated when the function is called Deallocated when the function returns

Automatic Variables void g() { int k[3]; int m; } void f(int i) { int j; (i>0) ? f(i-1) : g(); int main() { int x; f(1); return 0;

Automatic Variables void g() { int k[3]; int m; } void f(int i) { int j; (i>0) ? f(i-1) : g(); int main() { int x; f(1); return 0;

Automatic Variables void g() { int k[3]; int m; } void f(int i) { int j; (i>0) ? f(i-1) : g(); int main() { int x; f(1); return 0;

Automatic Variables void g() { int k[3]; int m; } void f(int i) { int j; (i>0) ? f(i-1) : g(); int main() { int x; f(1); return 0;

Dynamic Memory Allocated in response to an instruction Explicit: new operator or malloc call Implicit: appending to lists in Python Reside on the heap Lifecycle Allocated on demand Freed on demand

Dynamic Memory Dynamic memory is basically required for non-trivial programs Responding to external events User input, etc. Reading files of arbitrary size

Dynamic Memory But it has a dark side The programmer can forget to free the memory "Memory Leak" Or worse, memory can be accessed after it is freed "Dangling Pointers" These bugs can take a long time to track down because they don't necessarily fail right away.

Solution ! Take the responsibility for managing dynamic memory out of the hands of the developer Automatically free memory when it is no longer accessible to the program Memory can't be leaked (permanently, at least) Dangling pointers can't exist (by definition)

Two approaches Reference Counting Garbage Collection

Reference Counting Conceptually simple Every object is augmented with a count of the number of references pointing to it. Whenever a new reference is added, the count increases Whenever a reference is lost, the count decreases If the count reaches zero, the object is freed

Reference Counting Major Flaw Self-referential objects

An example in Python x = SomeClass()

An example in Python x = SomeClass() x.ref = x

An example in Python x = SomeClass() x.ref = x x=y

The object will never be freed! x = SomeClass() x.ref = x x=y

Garbage Collection Wait until memory is exhausted and then clean up any memory that can't be reached by any variables in the program "Mark-and-sweep" Initially mark all objects as "garbage" Walk the graph of references from the program variables, through anything reachable from those variables, and so on, marking anything reachable as "not garbage" Free anything still marked as "garbage"

Advantage The memory from the Python example will be freed

Disadvantage Waiting until all memory is exhausted causes very noticeable pauses in the program execution Whereas reference-counting interleaves object destruction with the normal execution

Workaround Observation: there are many short-lived objects and relatively few long-lived ones Generational garbage collection creates new objects in a "nursery" that is a smaller pool of memory for initial object allocation. Once it is exhausted, it is garbage collected as before, but because it is smaller, the effect is less noticeable.

Workaround Combining reference counting with garbage collection can provide the best of both approaches This is actually how Python works. It periodically checks for isolated cycles any container that can produce a reference cycle must support cycle detection