Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Similar documents
Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Allocation

Memory Allocation III

Dynamic Memory Allocation II October 22, 2008

Memory Allocation III

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Today. Dynamic Memory Allocation: Advanced Concepts. Explicit Free Lists. Keeping Track of Free Blocks. Allocating From Explicit Free Lists

Dynamic Memory Allocation. Gerson Robboy Portland State University. class20.ppt

Internal Fragmentation

CS 153 Design of Operating Systems

Dynamic Memory Allocation

Memory Allocation III CSE 351 Spring (original source unknown)

Memory Allocation III

Dynamic Memory Allocation: Advanced Concepts

Dynamic Memory Allocation: Advanced Concepts

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Allocation: Advanced Concepts

Dynamic Memory Allocation

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Caches II. CSE 351 Autumn Instructor: Justin Hsia

Binghamton University Dynamic Memory Alloca/on

Foundations of Computer Systems

Dynamic Memory Allocation

Dynamic Memory Allocation: Basic Concepts

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

CS 33. Storage Allocation. CS33 Intro to Computer Systems XXVII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Data III & Integers I

Dynamic Memory Alloca/on: Advanced Concepts

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

Arrays. CSE 351 Autumn Instructor: Justin Hsia

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Allocation: Basic Concepts

Dynamic Memory Allocation I Nov 5, 2002

Dynamic Memory Allocation I

Structs and Alignment

CSCI-UA /2. Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics

Dynamic Memory Allocation. Basic Concepts. Computer Organization 4/3/2012. CSC252 - Spring The malloc Package. Kai Shen

Virtual Memory II. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Allocation

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

Dynamic Memory Alloca/on: Basic Concepts

Dynamic Memory Alloca/on: Basic Concepts

Memory management. Johan Montelius KTH

10.1. CS356 Unit 10. Memory Allocation & Heap Management

Cache Example, System Control Flow

ANITA S SUPER AWESOME RECITATION SLIDES

Recitation #12 Malloc Lab - Part 2. November 14th, 2017

Review! Lecture 5 C Memory Management !

Dynamic Memory Alloca/on: Advanced Concepts

CS61C : Machine Structures

Foundations of Computer Systems

Lecture Notes on Garbage Collection

Memory Allocation III

Compiler Construction

Compiler Construction

x86 Programming III CSE 351 Autumn 2016 Instructor: Justin Hsia

CS61C : Machine Structures

Dynamic Memory Allocation. Zhaoguo Wang

CS61C : Machine Structures

Princeton University. Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

Run-time Environments -Part 3

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

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

Dynamic Memory Management

Java and C. CSE 351 Autumn 2018

Virtual Memory III. CSE 351 Autumn Instructor: Justin Hsia

Recitation #11 Malloc Lab. November 7th, 2017

Optimizing Dynamic Memory Management

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

Processes. CSE 351 Autumn Instructor: Justin Hsia

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

Dynamic Memory Management! Goals of this Lecture!

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

CMSC 330: Organization of Programming Languages

Dynamic Memory Management

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

CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM

Lecture 13: Complex Types and Garbage Collection

CS61C : Machine Structures

CS61C : Machine Structures

CMSC 330: Organization of Programming Languages

Lecture 13: Garbage Collection

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

Class Information ANNOUCEMENTS

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

Memory Management. Didactic Module 14 Programming Languages - EEL670 1

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

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

Carnegie Mellon. Malloc Boot Camp. Stan, Nikhil, Kim

Run-time Environments - 3

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

Princeton University Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

CS61, Fall 2012 Section 2 Notes

CS61 Section Notes. Section 5 (Fall 2011) Topics to be covered Common Memory Errors Dynamic Memory Allocation Assignment 3: Malloc

CS 153 Design of Operating Systems

The Stack & Procedures

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

Transcription:

Memory Allocation II CSE 351 Autumn 2016 Instructor: Justin Hsia Teaching Assistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun http://xkcd.com/1444/

Administrivia Homework 4 due Friday @ 11:45pm Lab 5 due Dec. 9 @ 11:45pm Final Exam: Tue, Dec. 13 @ 12:30pm in Kane 120 Combined for both lectures Review Session: Sun, Dec. 11 @ 1:30pm in EEB 105 Cumulative (midterm clobber policy applies) You get TWO double sided handwritten 8.5 sheets 11 cheat Recommended that you reuse or remake your midterm cheat sheet 2

Peer Instruction Question Which allocation strategy and requests removes external fragmentation in this Heap? B3 was the last fulfilled request. http://pollev.com/justinh (A) (B) Best fit: malloc(50), malloc(50) First fit: malloc(50), malloc(30) 50 10 30 10 50 B2 B3 (C) (D) Next fit: malloc(30), malloc(50) Next fit: malloc(50), malloc(30) 50 Start of heap B1 3

Implicit Free List Review Questions 16/0 16/0 16/1 16/124/0 24/0 16/1 16/1 What is the block header? What do we store and how? What are boundary tags and why do we need them? When we coalesce free blocks, how many neighboring blocks do we need to check on either side? Why is this? If I want to check the size of the th block forward from the current block, how many memory accesses do I make? 4

Keeping Track of Free Blocks = 4 byte word (free) = 4 byte word (allocated) 1) Implicit free list using length links all blocks using math No actual pointers, and must check each block if allocated or free 20 16 24 8 2) Explicit free list among only the free blocks, using pointers 20 16 24 8 3) Segregated free list Different free lists for different size classes 4) Blocks sorted by size Can use a balanced binary tree (e.g. red black tree) with pointers within each free block, and the length used as a key 5

Explicit Free Lists Allocated block: Free block: size a size a next payload and padding prev size a size a (same as implicit free list) Use list(s) of free blocks, rather than implicit list of all blocks The next free block could be anywhere in the heap So we need to store next/previous pointers, not just sizes Since we only track free blocks, so we can use payload for pointers Still need boundary tags (header/footer) for coalescing 6

Review: Doubly Linked Lists Linear Root Needs head/root pointer First node prev pointer is NULL Last node next pointer is NULL Good for first fit, best fit Start Circular Still have pointer to tell you which node to start with No NULL pointers (term condition is back at starting point) Good for next fit, best fit 7

Explicit Free Lists Logically: doubly linked list A B C Physically: blocks can be in any order A Forward (next) links 16 16 16 16 24 24 16 16 16 16 C B Back (prev) links 8

Allocating From Explicit Free Lists Note: These diagrams are not very specific about where inside a block a pointer points. In reality we would always point to one place (e.g. start/header of a block). Before After (with splitting) = malloc( ) 9

Allocating From Explicit Free Lists Note: These diagrams are not very specific about where inside a block a pointer points. In reality we would always point to one place (e.g. start/header of a block). Before After (fully allocated) = malloc( ) 10

Freeing With Explicit Free Lists Insertion policy: Where in the free list do you put the newly freed block? LIFO (last in first out) policy Insert freed block at the beginning (head) of the free list Pro: simple and constant time Con: studies suggest fragmentation is worse than the alternative Address ordered policy Insert freed blocks so that free list blocks are always in address order: address(previous) < address(current) < address(next) Con: requires linear time search Pro: studies suggest fragmentation is better than the alternative 11

Coalescing in Explicit Free Lists Case 1 Case 2 Case 3 Case 4 Block being freed Allocated Allocated Free Free Allocated Free Allocated Free Neighboring free blocks are already part of the free list 1) Remove old block from free list 2) Create new, larger coalesced block 3) Add new block to free list (insertion policy) How do we tell if a neighboring block if free? 12

Freeing with LIFO Policy (Case 1) Boundary tags not shown, but don t forget about them! Before free( ) Root Insert the freed block at the root of the list After Root 13

Freeing with LIFO Policy (Case 2) Boundary tags not shown, but don t forget about them! Before free( ) Root Splice successor block out of list, coalesce both memory blocks, and insert the new block at the root of the list After Root 14

Freeing with LIFO Policy (Case 3) Boundary tags not shown, but don t forget about them! Before free( ) Root Splice predecessor block out of list, coalesce both memory blocks, and insert the new block at the root of the list After Root 15

Freeing with LIFO Policy (Case 4) Boundary tags not shown, but don t forget about them! Before free( ) Root After Splice predecessor and successor blocks out of list, coalesce all 3 memory blocks, and insert the new block at the root of the list Root 16

Do we always need the boundary tag? Allocated block: Free block: size a size a next payload and padding prev size a (same as implicit free list) Lab 5 suggests no size a 17

Explicit List Summary Comparison with implicit list: Block allocation is linear time in number of free blocks instead of all blocks Much faster when most of the memory is full Slightly more complicated allocate and free since we need to splice blocks in and out of the list Some extra space for the links (2 extra pointers needed for each free block) Increases minimum block size, leading to more internal fragmentation Most common use of explicit lists is in conjunction with segregated free lists Keep multiple linked lists of different size classes, or possibly for different types of objects 18

Keeping Track of Free Blocks = 4 byte word (free) = 4 byte word (allocated) 1) Implicit free list using length links all blocks using math No actual pointers, and must check each block if allocated or free 20 16 24 8 2) Explicit free list among only the free blocks, using pointers 20 16 24 8 3) Segregated free list Different free lists for different size classes 4) Blocks sorted by size Can use a balanced binary tree (e.g. red black tree) with pointers within each free block, and the length used as a key 19

Segregated List (SegList) Allocators Each size class of blocks has its own free list Organized as an array of free lists Size class (in bytes) 8 16 24 32 40 inf Often have separate classes for each small size For larger sizes: One class for each two power size 20

SegList Allocator Have an array of free lists for various size classes To allocate a block of size : Search appropriate free list for block of size If an appropriate block is found: [Optional] Split block and place free fragment on appropriate list If no block is found, try the next larger class Repeat until block is found If no block is found: Request additional heap memory from OS (using sbrk) Place remainder of additional heap memory as a single free block in appropriate size class 21

SegList Allocator Have an array of free lists for various size classes To free a block: Mark block as free Coalesce (if needed) Place on appropriate class list 22

SegList Advantages Higher throughput Search is log time for power of two size classes Better memory utilization First fit search of seglist approximates a best fit search of entire heap Extreme case: Giving every block its own size class is no worse than best fit search of an explicit list Don t need to use space for block size for the fixed size classes 23

Allocation Policy Tradeoffs Data structure of blocks on lists Implicit (free/allocated), explicit (free), segregated (many free lists) others possible! Placement policy: first fit, next fit, best fit Throughput vs. amount of fragmentation When do we split free blocks? How much internal fragmentation are we willing to tolerate? When do we coalesce free blocks? Immediate coalescing: Every time free is called Deferred coalescing: Defer coalescing until needed e.g. when scanning free list for malloc or when external fragmentation reaches some threshold 24

More Info on Allocators D. Knuth, The Art of Computer Programming, 2 nd edition, Addison Wesley, 1973 The classic reference on dynamic storage allocation Wilson et al, Dynamic Storage Allocation: A Survey and Critical Review, Proc. 1995 Int l Workshop on Memory Management, Kinross, Scotland, Sept, 1995. Comprehensive survey Available from CS:APP student site (csapp.cs.cmu.edu) 25

Wouldn t it be nice If we never had to free memory? Do you free objects in Java? Reminder: implicit allocator 26

Garbage Collection (GC) (Automatic Memory Management) Garbage collection: automatic reclamation of heap allocated storage application never explicitly frees memory void foo() { int* p = (int*) malloc(128); return; /* p block is now garbage! */ } Common in implementations of functional languages, scripting languages, and modern object oriented languages: Lisp, Racket, Erlang, ML, Haskell, Scala, Java, C#, Perl, Ruby, Python, Lua, JavaScript, Dart, Mathematica, MATLAB, many more Variants ( conservative garbage collectors) exist for C and C++ However, cannot necessarily collect all garbage 27

Garbage Collection How does the memory allocator know when memory can be freed? In general, we cannot know what is going to be used in the future since it depends on conditionals But, we can tell that certain blocks cannot be used if they are unreachable (via pointers in registers/stack/globals) Memory allocator needs to know what is a pointer and what is not how can it do this? Sometimes with help from the compiler 28