Dynamic Memory Management

Similar documents
Dynamic Memory Management! Goals of this Lecture!

Dynamic Memory Management

CSC 1600 Memory Layout for Unix Processes"

Dynamic Memory Allocation

Optimizing Dynamic Memory Management

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

CS61C : Machine Structures

Dynamic Memory Allocation: Basic Concepts

Dynamic Memory Allocation: Basic Concepts

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

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

o Code, executable, and process o Main memory vs. virtual memory

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

Dynamic Memory Allocation

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

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

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

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

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

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

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

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

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

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

Limitations of the stack

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

Dynamic Memory Alloca/on: Basic Concepts

Dynamic Memory Allocation I Nov 5, 2002

Heap Management. Heap Allocation

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

Memory Management Basics

Dynamic Memory Allocation I

Dynamic Memory Allocation

Run-time Environments

Run-time Environments

Programming Language Implementation

Lectures 13 & 14. memory management

Dynamic Storage Allocation

Class Information ANNOUCEMENTS

CS61C : Machine Structures

10.1. CS356 Unit 10. Memory Allocation & Heap Management

Dynamic Memory Alloca/on: Basic Concepts

Review! Lecture 5 C Memory Management !

CMSC 330: Organization of Programming Languages

CS61C : Machine Structures

Memory Management in C (Dynamic Strings) Personal Software Engineering

Run-Time Environments/Garbage Collection

CMSC 330: Organization of Programming Languages

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

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

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Optimizing Dynamic Memory Management!

Lecture 13: Garbage Collection

Dynamic Memory Allocation

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Garbage Collection (1)

Run-time Environments -Part 3

ECE 598 Advanced Operating Systems Lecture 10

CS61C Midterm Review on C & Memory Management

CSE351 Spring 2018, Final Exam June 6, 2018

Requirements, Partitioning, paging, and segmentation

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

Memory: Overview. CS439: Principles of Computer Systems February 26, 2018

Robust Memory Management Schemes

Dynamic Memory Allocation. Zhaoguo Wang

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

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Requirements, Partitioning, paging, and segmentation

CS24 Week 2 Lecture 1

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Memory management. Johan Montelius KTH

Run-time Environments - 3

Lecture 8 Dynamic Memory Allocation

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

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

ECE 598 Advanced Operating Systems Lecture 12

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Advanced Programming & C++ Language

Engine Support System. asyrani.com

COSC Software Engineering. Lectures 14 and 15: The Heap and Dynamic Memory Allocation

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

Memory Management. To do. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

CS Computer Systems. Lecture 8: Free Memory Management

CS61C : Machine Structures

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

CS61C : Machine Structures

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

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

Memory Management. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

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

Memory Management. Today. Next Time. Basic memory management Swapping Kernel memory allocation. Virtual memory

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

Dynamic Data Structures. CSCI 112: Programming in C

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

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

CS61C : Machine Structures

Transcription:

Dynamic Memory Management Professor Jennifer Rexford http://www.cs.princeton.edu/~jrex 1 Goals of Today s Lecture Dynamic memory management o Garbage collection by the run-time system (Java) o Manual deallocation by the programmer (C, C++) Challenges of manual deallocation o Arbitrary request sizes in an arbitrary order o Complex evolution of heap as a program runs Design decisions for the K&R implementation o Circular linked-list of free blocks with a first fit allocation o Coalescing of adjacent blocks to create larger blocks 2 1

Memory Layout: char* string = hello ; int isize; char* f() { char* p; scanf( %d, &isize); p = malloc(isize); return p; Needed when required memory size is not known before the program runs Text RoData Data BSS 3 Allocating & Deallocating Memory Dynamically allocating memory o Programmer explicitly requests space in memory o Space is allocated dynamically on the heap o E.g., using malloc in C, and new in Java Dynamically deallocating memory o Must reclaim or recycle memory that is never used again o To avoid (eventually) running out of memory Garbage o Allocated block in heap that will not be accessed again o Can be reclaimed for later use by the program 4 2

Option #1: Garbage Collection Run-time system does garbage collection (Java) o Automatically determines objects that can t be accessed o And then reclaims the resources used by these objects Object x = new Foo(); Object y = new Bar(); x = new Quux(); if (x.check_something()) { x.do_something(y); System.exit(); Object Foo() is never used again! 5 Challenges of Garbage Collection Detecting the garbage is not always easy o if (complex_function(y)) x = Quux(); o Run-time system cannot collect all of the garbage Detecting the garbage introduces overhead o Keeping track of references to objects (e.g., counter) o Scanning through accessible objects to identify garbage o Sometimes walking through a large amount of memory Cleaning the garbage leads to bursty delays o E.g., periodic scans of the objects to hunt for garbage o Leading to unpredictable freeze of the running program o Very problematic for real-time applications o though good run-time systems avoid long freezes 6 3

Option #2: Manual Deallocation Programmer deallocates the memory (C and C++) o Manually determines which objects can t be accessed o And then explicitly returns the resources to the heap o E.g., using free in C or delete in C++ Advantages o Lower overhead o No unexpected pauses o More efficient use of memory Disadvantages o More complex for the programmer o Subtle memory-related bugs o Security vulnerabilities in the (buggy) code 7 Manual Deallocation Can Lead to Bugs Dangling pointers o Programmer frees a region of memory o but still has a pointer to it o Dereferencing pointer reads or writes nonsense values int main(void) { char *p; p = malloc(1); free(p); putchar(*p); May print nonsense character. 8 4

Manual Deallocation Can Lead to Bugs Memory leak o Programmer neglects to free unused region of memory o So, the space can never be allocated again o Eventually may consume all of the available memory void f(void) { char *s; s = malloc(5); return; Eventually, malloc() returns NULL int main(void) { while (1) f(); return ; 9 Manual Deallocation Can Lead to Bugs Double free o Programmer mistakenly frees a region more than once o Leading to corruption of the heap data structure o or premature destruction of a different object int main(void) { char *p, *q; p = malloc(1); free(p); q = malloc(1); free(p); Might free the space allocated to q! 1 5

Challenges for Malloc and Free Malloc() may ask for an arbitrary number of bytes Memory may be allocated & freed in different order Cannot reorder requests to improve performance 11 : Dynamic Memory xffffffff 12 6

: Dynamic Memory p2 xffffffff 13 : Dynamic Memory p2 p3 xffffffff 14 7

: Dynamic Memory p2 p3 xffffffff 15 : Dynamic Memory p2 p3 p4 xffffffff 16 8

: Dynamic Memory p2 p3 p4 xffffffff 17 : Dynamic Memory p5, p2 p3 p4 xffffffff 18 9

: Dynamic Memory p5, p2 p3 p4 xffffffff 19 : Dynamic Memory p5, p2 p3 p4 xffffffff 2 1

: Dynamic Memory p5, p2 p3 p4 xffffffff 21 Goals for Malloc and Free Maximizing throughput o Maximize number of requests completed per unit time o Need both malloc() and free() to be fast Maximizing memory utilization o Minimize the amount of wasted memory o Need to minimize size of data structures Strawman #1: free() does nothing o Good throughput, but poor memory utilization Strawman #2: malloc() finds the best fit o Good memory utilization, but poor throughput 22 11

Keeping Track of Free Blocks Maintain a collection of free blocks of memory o Allocate memory from one of the blocks in the free list o Deallocate memory by returning the block to the free list o Ask the OS for additional block when more are needed Design questions o How to keep track of the free blocks in memory? o How to choose an appropriate free block to allocate? o What to do with the left-over space in a free block? o What to do with a block that has just been freed? free free free 23 Need to Minimize Fragmentation Internal fragmentation o Allocated block is larger than malloc() requested o E.g., malloc() imposes a minimum size (e.g., 64 bytes) 33 External fragmentation o Enough free memory exists, but no block is big enough o E.g., malloc() asks for 128 contiguous bytes 64 64 64 24 12

Simple K&R-Like Approach Memory allocated in multiples of a base size o E.g., 16 bytes, 32 bytes, 48 bytes, Linked list of free blocks o Malloc() and free() walk through the list to allocate and deallocate Malloc() allocates the first big-enough block o To avoid sequencing further through the list Malloc() splits the free block o To allocate what is needed, and leave the rest available Linked list is circular o To be able to continue where you left off Linked list in the order the blocks appear in memory o To be able to coalesce neighboring free blocks 25 Allocate Memory in Multiples of Base Size Allocate memory in multiples of a base size o To avoid maintaining very tiny free blocks o To align memory on size of largest data type (e.g., long) Requested size is rounded up o Allocation in units of base_size o Round:(nbytes + base_size 1)/base_size Example: o Suppose nbytes is 37 o And base_size is 16 bytes o Then (37 + 16 1)/16 is 52/16 which rounds down to 3 16 16 5 26 13

Linked List of Free Blocks Linked list of free blocks Malloc() allocates a big-enough block Allocated Free() adds newly-freed block to the list Newly freed 27 First-Fit Allocation Handling a request for memory (e.g., malloc) o Find a free block that satisfies the request o Must have a size that is big enough, or bigger Simplest approach: first fit o Sequence through the linked list o Stop upon encountering a big enough free block Example: request for 64 bytes o First-fit algorithm stops at the 128-byte block 48 32 128 64 256 28 14

Splitting an Oversized Free Block Simple case: perfect fit o Malloc() asks for 128 bytes, and free block has 128 bytes o Simply remove the free block from the list 48 32 128 64 256 Complex case: splitting the block o Malloc() asks for 64 bytes, and free block has 128 bytes 48 32 64 64 256 64 29 Circular Linked List of Free Blocks Advantages of making free list a circular list o Any element in the list can be the beginning o Don t have to handle the end of the list as special Performance optimization o Make the head be where last block was found o More likely to find big enough blocks later in the list new head 48 32 64 64 256 3 15

Maintaining Free Blocks in Order Keep list in order of increasing addresses o Makes it easier to coalesce adjacent free blocks Though, makes calls to free() more expensive o Need to insert the newly-freed block in the right place Free list In use In use In use 31 Coalescing Adjacent Free Blocks When inserting a block in the free list o Look left and look right for neighboring free blocks In use In use Left In use Right In use In use 32 16

An Implementation Challenge Need information about each free block o Starting address of the block of memory o Length of the free block o Pointer to the next block in the free list Where should this information be stored? o Number of free blocks is not known in advance o So, need to store the information on the heap But, wait, this code is what manages the heap!!! o Can t call malloc() to allocate storage for this information o Can t call free() to relinquish the storage, either 33 Store Information in the Free Block Store the information directly in the free block o Since the memory isn t being used for anything anyway o And allows data structure to grow and shrink as needed Every free block has a header o Size of the free block o Pointer to (i.e., address of) the next free block size Challenge: programming outside the type system 34 17

Conclusions Elegant simplicity of K&R malloc and free o Simple header with pointer and size in each free block o Simple circular linked list of free blocks o Relatively small amount of code (~25 lines each) Limitations of K&R functions in terms of efficiency o Malloc requires scanning the free list To find the first free block that is big enough o Free requires scanning the free list To find the location to insert the to-be-freed block 35 18