CS S-11 Memory Management 1

Similar documents
CS61C : Machine Structures

CS61C : Machine Structures

Automatic Memory Management

CS61C : Machine Structures

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

CS61C : Machine Structures

CMSC 330: Organization of Programming Languages

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

CS61C : Machine Structures

Semantics (cont.) Symbol Table. Static Scope. Static Scope. Static Scope. CSE 3302 Programming Languages. Static vs. Dynamic Scope

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

In Java we have the keyword null, which is the value of an uninitialized reference type

Run-time Environments -Part 3

G Programming Languages - Fall 2012

Compiler Construction

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

(notes modified from Noah Snavely, Spring 2009) Memory allocation

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Run-time Environments - 3

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CSE 307: Principles of Programming Languages

Compiler Construction

Lecture 13: Garbage Collection

CS61C Midterm Review on C & Memory Management

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

Memory management. Johan Montelius KTH

10.1. CS356 Unit 10. Memory Allocation & Heap Management

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Data, memory. Pointers and Dynamic Variables. Example. Pointers Variables (or Pointers) Fall 2018, CS2

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Dynamic Storage Allocation

Garbage Collection. Akim D le, Etienne Renault, Roland Levillain. May 15, CCMP2 Garbage Collection May 15, / 35

Programming Language Implementation

CS61C : Machine Structures

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

Heap Management. Heap Allocation

CS61C : Machine Structures

Process s Address Space. Dynamic Memory. Backing the Heap. Dynamic memory allocation 3/29/2013. When a process starts the heap is empty

Compiler construction

CA31-1K DIS. Pointers. TA: You Lu

CPSC 213. Introduction to Computer Systems. Winter Session 2017, Term 2. Unit 1c Jan 24, 26, 29, 31, and Feb 2

Classes. Compiling Methods. Code Generation for Objects. Implementing Objects. Methods. Fields

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

Class Information ANNOUCEMENTS

CPSC 213. Introduction to Computer Systems. Instance Variables and Dynamic Allocation. Unit 1c

Run-Time Environments/Garbage Collection

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Lectures 13 & 14. memory management

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Dynamic Memory Management

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Review! Lecture 5 C Memory Management !

Compilers CS S-07 Building Abstract Assembly

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Dynamic Allocation in C

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018


Dynamic Memory Management! Goals of this Lecture!

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

Compiler Construction D7011E

CSC 1600 Memory Layout for Unix Processes"

CS61, Fall 2012 Section 2 Notes

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

Dynamic Memory Allocation II October 22, 2008

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Dynamic Memory Allocation

Lecture 15a Persistent Memory & Shared Pointers

CS842: Automatic Memory Management and Garbage Collection. Mark and sweep

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

CS 415 Midterm Exam Spring SOLUTION

Semantics. Names. Binding Time

Short Notes of CS201

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

Memory Management. Didactic Module 14 Programming Languages - EEL670 1

Memory Management. Chapter Fourteen Modern Programming Languages, 2nd ed. 1

ACM Trivia Bowl. Thursday April 3 rd (two days from now) 7pm OLS 001 Snacks and drinks provided All are welcome! I will be there.

Dynamic Allocation of Memory

CS201 - Introduction to Programming Glossary By

C Structures & Dynamic Memory Management

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

CS 241 Honors Memory

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Dynamic Memory Management

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

Lecture Outine. Why Automatic Memory Management? Garbage Collection. Automatic Memory Management. Three Techniques. Lecture 18

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Name, Scope, and Binding. Outline [1]

Lecture Notes on Garbage Collection

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Garbage Collection Algorithms. Ganesh Bikshandi

Performance of Non-Moving Garbage Collectors. Hans-J. Boehm HP Labs

Dynamic Memory Allocation: Advanced Concepts

CS 403 Compiler Construction Lecture 8 Syntax Tree and Intermediate Code Generation [Based on Chapter 6 of Aho2] This Lecture

Transcription:

CS414-2017S-11 Management 1 11-0: Three places in memor that a program can store variables Call stack Heap Code segment 11-1: Eecutable Code Code Segment Static Storage Stack Heap 11-2: Three places in memor that a program can store variables Call stack Heap Local Variables Dnamicall allocated variables (Most of the variables in Java) Code segment 11-3: Static Storage Static variables If a variable is declared static, there is onl one instance of the variable Variable is tpicall stored in the code segment, not the stack or the heap Wh? 11-4: Static Storage If a variable is declared static, there is onl one instance of the variable

CS414-2017S-11 Management 2 Variable is tpicall stored in the code segment, not the stack or the heap Stack storage is too transient Using the code segment guarantees a single instance of the variable 11-5: Static Storage class StaticVars { static int ; void main() { StaticVars SV1 = new StaticVars(); StaticVars SV2 = new StaticVars(); SV1. = 1; SV1. = 2; SV2. = 3; SV2. = 4; print(sv1.); print(sv1.); print(sv2.); print(sv2.); 11-6: Static Storage class StaticVars { static int ; void main() { StaticVars SV1 = new StaticVars(); StaticVars SV2 = new StaticVars(); SV1. = 1; SV1. = 2; SV2. = 3; SV2. = 4; print(sv1.); print(sv1.); print(sv2.); print(sv2.); Output: 1 4 3 4 11-7: simplejava Static Storage What do we need to do to implement static storage in simplejava? 11-8: simplejava Static Storage What do we need to do to implement static storage in simplejava? Looking at each portion of the compiler in turn: Leical Analsis what needs to be done? 11-9: simplejava Static Storage Leical Analsis Add a new keword static to the language Add static token 11-10: simplejava Static Storage Parsing & Building AST 11-11: simplejava Static Storage

CS414-2017S-11 Management 3 Parsing & Building AST Add a static tag to the AST for variable declarations (for both statements, and class instance variables) 11-12: simplejava Static Storage Semantic Analsis 11-13: simplejava Static Storage Semantic Analsis No changes are necessar (apart form changes needed to implement building Abstract Assembl Tree) 11-14: simplejava Static Storage Abstract Assembl Tree Generation 11-15: simplejava Static Storage Abstract Assembl Tree Generation Add a new field to variable entries static bit Generate code for static variables Need to access variables in the code segment Need to be able to access a direct address 11-16: simplejava Static Storage Need to access a direct address Add an AddressEp node to our AAT Single child assembl language label Represents the memor location at that address 11-17: simplejava Static Storage static AddressEp Label("001") 11-18: simplejava Static Storage class { static int ; class C2 { int a; class1;... C2 class2;

CS414-2017S-11 Management 4 AAT forclass2.class1.? 11-19: simplejava Static Storage Operator(-) Constant(_offset) Operator(-) Constant(class1_offset) Operator(-) Register(FP) Constant(class2_offset) 11-20: simplejava Static Storage class { static int ; class C2 { int a; class1;... C2 class2; AAT forclass2.class1.? 11-21: simplejava Static Storage AddressEp Label("001") 11-22: simplejava Static Storage Code Generation 11-23: simplejava Static Storage Code Generation Add space to code segment to store static variables Make sure the labels match!!

CS414-2017S-11 Management 5 11-24: Heap-based storage There are 2 main memor-allocation dangers associated with heap-based storage Dangling References leaks 11-25: Dangling References int main() { int *a; int *b; a = (int *) malloc(sizeof(int)); (*a) = 4; b = a; free b;... What happens if we change(*a) [(*a) =...]? 11-26: Leaks int main() { int *intptr; intptr = (int *) malloc(sizeof(int)); intptr = NULL;... Allocated memor that we can t get to garbage Eventuall, use up heap memor 11-27: Managing the Heap Manage the heap to avoid memor leaks and dangling references Give all decisions to the programmer Automatic memor management 11-28: Programmer Controlled Advantages 11-29: Free List management sstem is less complicated Lower run-time overhead for the memor manager Can manage the memor needs for a specific program more efficientl than a general-purpose memor manager (at least in theor) List of all available blocks of memor

CS414-2017S-11 Management 6 When a request for a block of memor is made, it is removed from the free list Deallocated memor is returned to the free list 11-30: Free List Housekeeping When a block is requested, allocated slightl more memor than requested. Etra space is used to store header information (for now, just the size of the allocated block) Return a pointer to just after the header information Size of allocated block Pointer retuned to program requesting memor 11-31: Free List Eample class oneelem { class twoelem { int ; oneelem A = new oneelem(); oneelem B = new oneelem(); twoelem C = new twoelem(); twoelem D = new twoelem(); 11-32: Free List Eample Freelist 1024

CS414-2017S-11 Management 7 11-33: Free List Eample Freelist Allocated 984 11-34: Free List Eample class oneelem { class twoelem { int ; oneelem A = new oneelem(); oneelem B = new oneelem(); twoelem C = new twoelem(); twoelem D = new twoelem(); delete A; delete C; 11-35: Free List Eample

CS414-2017S-11 Management 8 Freelist 8 Allocated 12 Allocated 984 11-36: Free List Eample class oneelem { class twoelem { int ; oneelem A = new oneelem(); oneelem B = new oneelem(); twoelem C = new twoelem(); twoelem D = new twoelem(); delete A; delete C; delete D; 11-37: Free List Eample

CS414-2017S-11 Management 9 Freelist 8 Allocated 12 12 984 11-38: Free List Eample Freelist 8 Allocated 1008 11-39: First Fit When there are several blocks to choose from on the free list, which do we use to fulfill a memor request? First Fit Return the first block that is large enough 11-40: First Fit class smallclass { void main() { int i; smallclass A[] = new smallclass[3000]; smallclass B[] = new smallclass[3000]; for (i=0; i<3000; i++) B[i] = new smallclass(); for (i=0; i<3000; i = i + 2)

CS414-2017S-11 Management 10 delete B[i]; delete A; /* Point A */ for (i=0; i<3000; i = i + 2) B[i] = new smallclass(); /* Point B */ 11-41: First Fit At Point A: 11-42: First Fit At Point B: 11-43: First Fit Plent of space on the heap Divided into small blocks can t service a request for a large block of memor fragmentation

CS414-2017S-11 Management 11 11-44: Best Fit When there are several blocks to choose from on the free list, which do we use to fulfill a memor request? First Fit Return the first block that is large enough Best Fit Return the smallest block that is large enough 11-45: Best Fit At Point B (using Best Fit): 11-46: Best Fit Best Fit will usuall lead to less memor fragmentation than first fit Don t waste large memor blocks on small requests Large blocks should then be available when needed Will Best Fit alwas lead to less memor fragmentation? 11-47: Best Fit vs First Fit for (i=0; i<100;i++) A[i] = malloc(4); for (i=0;i<100;i++) B[i] = malloc(3); for (i=0;i<100;i+=2) free(a[i]); for (i=0;i<100;i+=2) free(b[i]); for (i=0; i<100; i++) C[i] = malloc(2);

CS414-2017S-11 Management 12 11-48: Segregated Free List Fragmentation problems caused b differing block sizes Remove the problem b having all blocks be the same size (like lisp) Can t make all blocks the same size Can use a limited # of standard block sizes 11-49: Segregated Free List can onl be allocated in set block sizes Tpicall powers of 2 2 words, 4 words, 8 words, etc Separate free list maintained for each bock size When a request is made, the smallest block that can service the request is returned. 11-50: Segregated Free List Free List Arra 11-51: Segregated Free List Initiall, all heap memor is placed in the largest block list If a request is made for a block of memor of size 2 k, and list k is empt: Split a block from list k +1 into two blocks of size2 k Add these two blocks to list k List k is no longer empt can service the request 11-52: Segregated Free List Free List Arra 2 word blocks 4 word blocks 8 word blocks 16 word blocks 32 word blocks

CS414-2017S-11 Management 13 A request for a block of size 16 is made 11-53: Segregated Free List Free List Arra 2 word blocks 4 word blocks 8 word blocks 16 word blocks 32 word blocks 11-54: Segregated Free List Free List Arra 2 word blocks 4 word blocks 8 word blocks 16 word blocks 32 word blocks A request for a block of size 2 is made 11-55: Segregated Free List Free List Arra 2 word blocks 4 word blocks 8 word blocks 16 word blocks 32 word blocks 11-56: Garbage Collection Giving the user control of deallocation has problems: Writing programs that properl deallocate memor is hard Often, there are man pointers to the same block of memor (much like our current project!) It can be difficult to determine when a block of memor should be freed We don t want to be too aggressive in freeing memor (wh not?) 11-57: Garbage Collection Giving the user control of deallocation has problems: Writing programs that properl deallocate memor is hard Often, there are man pointers to the same block of memor (much like our current project!) It can be difficult to determine when a block of memor should be freed

CS414-2017S-11 Management 14 We don t want to be too aggressive in freeing memor (wh not?) Solution don t let programmer control deallocation! 11-58: Garbage Collection Don t allow programmer to deallocate an memor Garbage will collect Periodicall collect the accumulated garbage, and return it to the free list 11-59: Mark & Sweep When Garbage Collection routine is invoked: Mark all heap memor that is reachable b the program Need to add a mark bit to each block of memor can use the header Sweep through the entire block of memor, moving unmarked blocks to the free list 11-60: Mark Phase for each pointer P on the stack mark(p) mark(p) { if ((P is not null) and (mark bit of Mem[P] is not set)) set mark bit of Mem[P] for each pointer Q in the block Mem[p] mark(q) 11-61: Mark & Sweep class Class1 { int ; class Class2 { Class1 class Class3 { Class1 ; Class2 C2; Class3 C3 = new Class3(); C3. = new Class1(); C3.C2 = new Class2(); C3.C2. = new Class1(); 11-62: Mark & Sweep C3 Saved Registers SP Stack Heap Freelist C2

CS414-2017S-11 Management 15 11-63: Mark & Sweep class Class1 { int ; class Class2 { Class1 class Class3 { Class1 ; Class2 C2; Class3 C3 = new Class3(); C3. = new Class1(); C3.C2 = new Class2(); C3.C2. = new Class1(); C3. = new Class1(); 11-64: Mark & Sweep C3 Saved Registers SP Stack Heap Freelist C2 11-65: Mark & Sweep C3 Saved Registers SP Stack Heap Freelist C2 11-66: Mark & Sweep

CS414-2017S-11 Management 16 Stack Heap C3 Saved Registers SP Freelist C2 11-67: Whither Pointers? In order for the Mark phase to work correctl, we need to know which memor locations are pointers, and which are not Tag pointers Assume that an value that might be a pointer is a pointer (Conservative garbage collection) Create tables that store memor locations of all pointers 11-68: Tagging Pointers If we wish to tag pointers themselves, we have two options: Tag the pointer itself (high order bits) Store tag in preceding word 11-69: Tagging Pointers Tag the pointer itself (high order bits) If the high order bits are 11, then the memor location represents a pointer If the high oder bits are 00, 01, or 10, then the memor location represents an integer or boolean value Using 32-bit words, onl 30 bits will be available for pointer values Need to strip the tag before pointers can be dereferenced Using 32-bit words, slightl more than 31 bits are available for integer values (ver large negative values prohibited) 11-70: Tagging Pointers Store tag in preceding word Set aside a specific bit pattern as a sentinel value (something like -MAXINT) Ever pointer requires 2 words of storage word for the sentinel, and a word for the pointer itself 11-71: Tagging Pointers

CS414-2017S-11 Management 17 class Class1 { Class2 C2; Class3 C3; boolean ; Sentinel Sentinel C2 C3 11-72: Conservative GC Assume that ever memor location that could be a pointer is a pointer The integer will be considered a pointer if: Heap addresses are in the range LOW.. HIGH, and LOW = = HIGH The memor location is the beginning of an allocated block 11-73: Conservative GC Ever memor block on the heap that is pointed to b something on the stack will be marked No dangling references Some memor blocks on the heap that are not pointed to b something on the stack ma be marked Ma have some uncollected garbage Since no etra information (tagged pointers, etc.) is needed, Conservative Garbage Collectors can be run on languages not designed with garbage collection in mind (i.e., C) 11-74: Pointer Tables Create a table for each function & class, which keeps track of where the pointers are in that function or class This can be done at compile time Each function & class will need a kind field, to store what kind of function or class it is (classes will need a kind field anwa, if we want instanceof to work) 11-75: Pointer Tables class ClassA { int w; class ClassB { int ; ClassA ; ClassA C2; int z; void main() { int a;

CS414-2017S-11 Management 18 ClassA ; int b; ClassB C2; = new ClassA(); C2 = new ClassB(); C2. = new ClassA(); C2.C2 = new ClassA(); /* Bod of main */ 11-76: Pointer Tables Stack Ptr Table a b C2 Size Ptr Table w Heap 16 Size 24 Ptr Table C2 z Size 16 Ptr Table w Size 16 Ptr Table w Code Segment 2 # of pointers 8 16 0 # of pointers 2 # of pointers 8 12 main ClassA ClassB 11-77: Reference Counts Each block of allocated memor contains a count of how man pointers point to it Each time a pointer appears on the LHS of an assignment: Count of what the pointer used to point to is decremented Count of what the pointer now points to is incremented When a count hits zero, add block back to free list 11-78: Reference Count Problems From the Jargon File: (aka Hacker s Dictionar) One da a student came to Moon and said: I understand how to make a better garbage collector. We must keep a reference count of the pointers to each cons (block of memor). Moon patientl told the student the following stor: One da a student came to Moon and said: I understand how to make a better garbage collector...