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

Similar documents
CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

CS61, Fall 2012 Section 2 Notes

Structure of Programming Languages Lecture 10

CS61C : Machine Structures

Some major graph problems

Lecture 13: Garbage Collection

CS 241 Honors Memory

Automatic Memory Management

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

Garbage Collection. Steven R. Bagley

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.

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

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

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

CS S-11 Memory Management 1

Garbage Collection (1)

Lecture Notes on Garbage Collection

Linked lists. Prof. Noah Snavely CS1114

Robust Memory Management Schemes

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

Compiler Construction D7011E

CS61C : Machine Structures

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

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

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

Garbage Collection Algorithms. Ganesh Bikshandi

Heap Management portion of the store lives indefinitely until the program explicitly deletes it C++ and Java new Such objects are stored on a heap

Garbage Collection Techniques

CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers

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.

Automatic Garbage Collection

Dynamic Memory Management

CS61C : Machine Structures

CS61C : Machine Structures

CS61C : Machine Structures

Managed runtimes & garbage collection. CSE 6341 Some slides by Kathryn McKinley

Reference Counting. Reference counting: a way to know whether a record has other users

Managed runtimes & garbage collection

CS 4120 Lecture 37 Memory Management 28 November 2011 Lecturer: Andrew Myers

CS61C Midterm Review on C & Memory Management

Run-time Environments - 3

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

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

Depth First Search A B C D E F G A B C 5 D E F 3 2 G 2 3

19 File Structure, Disk Scheduling

CSE P 501 Compilers. Memory Management and Garbage Collec<on Hal Perkins Winter UW CSE P 501 Winter 2016 W-1

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

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

Dynamic Memory Management! Goals of this Lecture!

Run-time Environments -Part 3

Dynamic Storage Allocation

Reference Counting. Reference counting: a way to know whether a record has other users

CS125 : Introduction to Computer Science. Lecture Notes #11 Procedural Composition and Abstraction. c 2005, 2004 Jason Zych

Exploiting the Behavior of Generational Garbage Collector

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

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization

A deep dive into QML memory management internals

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

Run-Time Environments/Garbage Collection

CIS Operating Systems Contiguous Memory Allocation. Professor Qiang Zeng Spring 2018

Linked Lists

Book keeping. Will post HW5 tonight. OK to work in pairs. Midterm review next Wednesday

Garbage Collection. Weiyuan Li

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

Garbage Collection. CS 351: Systems Programming Michael Saelee

Dynamic Memory Allocation

CSCI-1200 Data Structures Spring 2018 Lecture 25 Garbage Collection & Smart Pointers

Shenandoah: An ultra-low pause time garbage collector for OpenJDK. Christine Flood Principal Software Engineer Red Hat

CS510 Operating System Foundations. Jonathan Walpole

CS Operating Systems

CS Operating Systems

Dynamic Memory Management

Implementation Garbage Collection

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

Programming Language Implementation

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

6. Pointers, Structs, and Arrays. 1. Juli 2011

Dynamic Memory Allocation: Advanced Concepts

CS1114 Assignment 3. 1 Previously, on Assignment 2. 2 Linked lists

CIS Operating Systems Memory Management. Professor Qiang Zeng Fall 2017

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

Run Time Environment

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

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

1 Dynamic Memory continued: Memory Leaks

Hard Real-Time Garbage Collection in Java Virtual Machines

3. Memory Management

Garbage Collection. Hwansoo Han

Lecture 8 Dynamic Memory Allocation

Lecture 23: Object Lifetime and Garbage Collection

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

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Optimizing Dynamic Memory Management

Review! Lecture 5 C Memory Management !

CIS Operating Systems Non-contiguous Memory Allocation. Professor Qiang Zeng Spring 2018

CS61C : Machine Structures

Transcription:

CS 1114: Data Structures memory allocation! Prof. Graeme Bailey http://cs1114.cs.cornell.edu (notes modified from Noah Snavely, Spring 2009) Memory allocation! Strategy 1: Computer keep tracks of free space at the end! Strategy 2: Computer keeps a linked list of free storage blocks ( freelist ) For each block, stores the size and location When we ask for more space, the computer finds a big enough block in the freelist What if it doesn t find one? 2

Maintaining a freelist 1 2 3 4 5 6 7 8 9 10 11 12 13 7 4 2 7 5 0 0 9 4 X X X X Free list Start: 10 Free space: 999,990 Delete the last element 1 2 3 7 7 1 4 5 6 X X X 7 8 9 0 9 0 10 11 12 13 X X X X Free list Start: 4 Free space: 3 Start: 10 Free space: 999,990 3 Allocation issues! Surprisingly important question: Which block do you supply? The smallest one that the users request fits into? A larger one, in case the user wants to grow the array? 4

Memory deallocation! How do we give the computer back a block we re finished with?! Someone has to figure out that certain values will never be used ever ( garbage ), and should be put back on the free list If this is too conservative, your program will use more and more memory ( memory leak ) If it s too aggressive, your program will crash ( blue screen of death ) 5 Memory deallocation! Two basic options: 1. Manual storage reclamation Programmer has to explicitly free garbage Languages: C, C++, assembler 2. Automatic storage reclamation Computer will notice that you re no longer using cells, and recycle them for you Languages: Matlab, Java, C#, Scheme 6

Manual storage reclamation! Programmers always ask for a block of memory of a certain size In C, explicitly declare when it is free! Desirable but complex invariants: 1. Everything should be freed when it is no longer going to be used 2. If we free something, we shouldn t try to use it again 3. And, it should be freed exactly once! 4. Minimize fragmentation 7 Automatic storage reclamation! Garbage collection! 1 st challenge: find memory locations that are still in use by the programmer ( live ) 1. Anything that has a name the programmer can get to (the root set ) 2. Anything pointed to by a live object Root set in Matlab 8

Garbage collection 1 2 3 4 5 6 7 8 9 10 11 12 13 11 X 2 4 5 1 0 8 3 15 1 6 5 3 Y 14 15 16 7 1 0! Two lists, X and Y! Which cells are live?! Which cells are garbage? 9 Simple algorithm: mark-sweep! Mark: Chase the pointers from the root set, marking everything as you go! Sweep: Scan all of memory everything not marked is garbage, and can go back on the free list 10

Mark and sweep 1 2 3 4 5 6 7 8 9 10 11 12 13 11 X 2 4 5 1 0 8 3 15 1 6 5 3 Y 14 15 16 7 1 0 Root set: { X, Y }! Mark phase! Sweep phase 11 Mark and sweep! The machine needs to be able to tell where the pointers are (we ll assume that it s up to the programmer to do that) For instance, the programmer will say that the second entry in a cell is a pointer (for singlylinked list) Or, for a doubly-linked list, the first and third entries in a cell are pointers 12

Mark and sweep! In general, pointers may have a complex structure How do we mark, in the general case? 13 When to do garbage collection?! Option 1 ( stop the world ): Once memory is full, stop everything and run garbage collection Your program will freeze while the garbage is being collected Not good if you re coding the safety monitoring system for a nuclear reactor! Option 2 ( incremental GC ): Collect garbage in parallel with the main program Needs to be careful not to step on the program 14

Linked lists running time! What about inserting / deleting from somewhere near the middle of the list? How can we find items in a list faster than simply starting at one or other end?! How can we fix this?! You ll have to wait a bit to learn about this. " 15 Where are we in the story? London We started with images Oslo Frankfurt Then sought blobs Paris Then decided that blobs are connected, so Built graphs Berlin Rome Then wanted to find the connected components, so Looked at implementing dfs and bfs, so Looked at implementing stacks and queues Then found that arrays were inefficient, so Looked at linked lists Then looked at implementing linked lists Arrays were messy, could we use a graph? Stockholm Vienna Prague 16

Representing a graph London Paris Build a big 2-D array Mentally label the rows and columns by the nodes Then insert a 0 if there are Rome no edges between those nodes, or a 1 if they are connected Frankfurt Berlin Oslo Stockholm Vienna Prague What does this array look like if there are lots of edges? there are very few edges? (we call these matrices sparse ) 17 Representing a graph Can we do better? London Paris Frankfurt Oslo Stockholm Build an array of linked lists! One linked list per node The edges live in the list Rome Berlin Vienna Prague Details: For each node A, put the nodes which are connected to that node into A s linked list - no longer sparse - and space isn t being wasted! - can represent directed graphs easily 18