Linked lists and memory allocation Prof. Noah Snavely CS1114

Slides:



Advertisements
Similar presentations
CHP-5 LinkedList.
Advertisements

Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Chapter 2: Memory Management, Early Systems
Chapter 2: Memory Management, Early Systems
ICS220 – Data Structures and Algorithms Lecture 13 Dr. Ken Cosh.
PZ10B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10B - Garbage collection Programming Language Design.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
CPSC 388 – Compiler Design and Construction
CS 536 Spring Automatic Memory Management Lecture 24.
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 Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
CS 1114: Data Structures – Implementation: part 1 Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Memory Management Memory Areas and their use Memory Manager Tasks:
Linked lists Prof. Noah Snavely CS1114
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.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
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 Management Last Update: July 31, 2014 Memory Management1.
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.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Storage allocation issues Prof. Ramin Zabih
OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg,
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Memory Management. Memory  Commemoration or Remembrance.
Garbage Collection and Memory Management CS 480/680 – Comparative Languages.
GARBAGE COLLECTION IN AN UNCOOPERATIVE ENVIRONMENT Hans-Juergen Boehm Computer Science Dept. Rice University, Houston Mark Wieser Xerox Corporation, Palo.
Memory Management -Memory allocation -Garbage collection.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
Breadth-first and depth-first traversal Prof. Noah Snavely CS1114
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
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)
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
Memory Management CSCI 2720 Spring What is memory management? “the prudent utilization of this scarce resource (memory), whether by conservation,
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Section 10: Memory Allocation Topics
Memory Management Memory Areas and their use Memory Manager Tasks:
Storage 18-May-18.
Chapter 2 Memory and process management
Prof. Ramin Zabih Implementing queues Prof. Ramin Zabih
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Memory Management 6/20/ :27 PM
Dynamic Memory Allocation
Storage Management.
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Memory Management Memory Areas and their use Memory Manager Tasks:
Automatic Memory Management
Garbage collection; lightstick orientation
Data Structures – Stacks and Queus
Simulated Pointers.
Chapter 12 Memory Management
Data Structures and Algorithms
Presentation made by Steponas Šipaila
Linked lists Prof. Noah Snavely CS1114
Memory Management Memory Areas and their use Memory Manager Tasks:
CS703 - Advanced Operating Systems
Data Structures and Algorithms
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Presentation transcript:

Linked lists and memory allocation Prof. Noah Snavely CS1114

Administrivia  Assignment 3 has been posted –Due next Friday, March 6  Prelim 1 Thursday in class –Review session this evening at 7:15pm, Upson 315 –Review session tomorrow, 8:30pm? –Topics include: running time, sorting, selection, graphs, connected components, linked lists –Closed-book, closed-note 2

Administrivia  Homework policy:  You can discuss problems in general with other students  You must write the code on your own – you may not share code 3

Bubble sort  What is the running time?  Which is faster? a)Bubble sort b)Quicksort 4

5 Linked lists  Alternative to an array  Every element (cell) has two parts: 1.A value (as in an array) 2.A link (address, pointer) to the next cell This pointer will always point to the start of a cell

6 Linked lists 8413 Values Links

7 Example X X

8 Doubly linked lists

9 A doubly-linked list in memory First element Last element Size of list XX

Doubly-linked list insertion 10 Initial list Insert a 5 at end Insert an 8 at the start XXX 456 XXX 789 XXXX XXX 789 XXXX XXXX

Memory allocation  When we need a new cell, how do we know where to find it?  We’ll keep track of a “free pointer” to the next unused cell after the list First element Last element Size of list XX Next free cell

Doubly-linked list insertion 12 Insert a 9 at the start XXX 789 XXXX XXXX Delete the last element XXXX

13 Memory allocation  Current strategy: when we need more storage, we just grab locations at the end  What can go wrong?  When we delete items from a linked list we change pointers so that the items are inaccessible –But they still waste space!

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? 14

Maintaining a freelist XXXX Delete the last element XXX XXXX Start: 10 Free space: 999,990 Start: 10 Free space: 999,990 Start: 4 Free space: 3 Free list

16 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?

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”) 17

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 18

19 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

20 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

Garbage collection  Two lists, X and Y  Which cells are live?  Which cells are garbage? XY

22 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

Mark and sweep  Mark phase  Sweep phase XY Root set: { X, Y }

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 singly- linked list) –Or, for a doubly-linked list, the first and third entries in a cell are pointers 24

Mark and sweep  In general, pointers may have a complex structure 25 How do we mark, in the general case?

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 26

Assignment 3  Implementing stacks and queues using linked lists  Using DFS and BFS to find connected components  Guiding the robot with the lightstick 27