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

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

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

Dynamic Memory Allocation: Basic Concepts

Dynamic Memory Allocation: Basic Concepts

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

Dynamic Memory Allocation

Dynamic Memory Allocation I Nov 5, 2002

Dynamic Memory Allocation I

Dynamic Memory Alloca/on: Basic Concepts

Dynamic Memory Alloca/on: Basic Concepts

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

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

CS 153 Design of Operating Systems

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

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

Dynamic Memory Allocation

Dynamic Memory Allocation

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

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

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Foundations of Computer Systems

Dynamic Memory Allocation

Binghamton University Dynamic Memory Alloca/on

Foundations of Computer Systems

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Allocation: Advanced Concepts

ANITA S SUPER AWESOME RECITATION SLIDES

Dynamic Memory Allocation. Zhaoguo Wang

Dynamic Memory Management

10.1. CS356 Unit 10. Memory Allocation & Heap Management

Dynamic Memory Management! Goals of this Lecture!

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

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

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 Management

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Internal Fragmentation

Optimizing Dynamic Memory Management

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

CS61C : Machine Structures

Many of the slides in this lecture are either from or adapted from slides provided by the authors of the textbook Computer Systems: A Programmer s

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

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

Dynamic Memory Allocation II October 22, 2008

Memory management. Johan Montelius KTH

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

CS 153 Design of Operating Systems

CS61C : Machine Structures

CS61C : Machine Structures

CS61C : Machine Structures

ECE 598 Advanced Operating Systems Lecture 10

ECE454, Fall 2014 Homework3: Dynamic Memory Allocation Assigned: Oct 9th, Due: Nov 6th, 11:59PM

Operating System Labs. Yuanbin Wu

Lectures 13 & 14. memory management

Dynamic Memory Allocation: Advanced Concepts

Dynamic Memory Allocation

Heap Management. Heap Allocation

CMSC 313 Spring 2010 Exam 3 May 17, 2010

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

Dynamic Memory Allocation: Advanced Concepts

CS 213, Fall 2002 Malloc Lab: Writing a Debugging Dynamic Storage Allocator Assigned: Fri Nov. 1, Due: Tuesday Nov. 19, 11:59PM

Dynamic Memory Alloca/on: Advanced Concepts

COMP 321: Introduction to Computer Systems

CSE351 Spring 2018, Final Exam June 6, 2018

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

Spring 2016, Malloc Lab: Writing Dynamic Memory Allocator

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

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

CS 2461: Computer Architecture I: Heap: Dynamic Memory Allocation Data Structures. Recap. Variables and Structures

1 Introduction. 2 Logistics. 3 Hand Out Instructions

CS 105 Malloc Lab: Writing a Dynamic Storage Allocator See Web page for due date

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

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

Secure Software Programming and Vulnerability Analysis

CSE 361S Intro to Systems Software Final Project

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

Lecture 13: Garbage Collection

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

CS201: Lab #4 Writing a Dynamic Storage Allocator

Engine Support System. asyrani.com

Concurrent Programming

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

Concurrent Programming

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

Class Information ANNOUCEMENTS

Memory Allocation III CSE 351 Spring (original source unknown)

HW 3: Malloc CS 162. Due: Monday, March 28, 2016

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

CS61C : Machine Structures

CS61C : Machine Structures

Memory Allocation. General Questions

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

mmalloc The GNU memory-mapped malloc package Fred Fish Cygnus Support Mike Haertel Free Software Foundation

2/9/18. Secure Coding. CYSE 411/AIT681 Secure Software Engineering. Agenda. Dynamic Memory Interface. Dynamic Memory Interface

Memory (Stack and Heap)

Memory Allocation III

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

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

Transcription:

Dynamic Memory Allocation: Basic Concepts Kai Shen Dynamic Memory Allocation Programmers use dynamic memory allocators (such as malloc) to acquire memory at run time. For data structures whose size is only known at runtime. Dynamic memory allocators manage an area of process virtual memory known as the heap. Application Dynamic Memory Allocator Heap User stack Heap (via malloc) Uninitialized data (.bss) Initialized data (.data) Program text (.text) Top of heap (brk ptr) 1 0 Dynamic Memory Allocation The malloc Package Allocator maintains heap as collection of variable sized blocks, which are either allocated or free Types of allocators Explicit allocator: application allocates and frees space E.g., malloc and free in C Implicit allocator: application allocates, but does not free space E.g. garbage collection in Java, ML, and Lisp We discuss explicit memory allocation today #include <stdlib.h> void *malloc(size_t size) Successful: Returns a pointer to a memory block of at least size bytes (typically) aligned to 8 byte boundary If size==0, returns NULL Unsuccessful: returns NULL (0) and sets errno void free(void *p) Returns the block pointed at by p to pool of available memory p must come from a previous call to malloc or realloc 3 4 CSC5 - Spring 01 1

Allocation Example p1 = malloc(4) p = malloc(5) p3 = malloc(6) free(p) p4 = malloc() Applications Constraints Can issue arbitrary sequence of malloc and free requests free request must be to a malloc d block Allocators Must respond immediately to malloc requests Can manipulate and modify only free memory Must allocate blocks from free memory Must align blocks so they satisfy all alignment requirements 8 byte alignment for GNU malloc (libc malloc) ) on Linux boxes Can t move the allocated blocks once they are malloc d i.e., compaction is not allowed 5 6 Interaction with OS Performance Goal: Runtime Speed Change heap size brk system call User stack Heap (via malloc) Uninitialized data (.bss) Initialized data (.data) Program text (.text) Top of heap (brk ptr) Given some sequence of malloc and free requests: R 0, R 1,..., R k,..., R n 1 Speed throughput: Number of completed requests per unit time Example: 5,000 malloc calls and 5,000 free calls i0 seconds Throughput is 1,000 operations/second 7 8 CSC5 - Spring 01

Performance Goal: Memory Utilization Given some sequence of malloc and free requests: R 0, R 1,..., R k,..., R n 1 Def: Aggregate payload P k malloc(p) results in a block with a payload of p bytes After request R k has completed, the aggregate payload P k is the sum of currently allocated payloads Fragmentation Poor memory utilization caused by fragmentation internal fragmentation external fragmentation Def: Heap size H k Maximum heap size so far Def: Peak memory utilization after k requests U k = ( max i<k P i ) / H k Goals: maximize runtime speed and memory utilization Somewhat conflicting 9 10 Internal Fragmentation External Fragmentation For a given block, internal fragmentation occurs if payload is smaller than block size Block Occurs when there is enough aggregate heap memory, but no single free block is large enough Internal fragmentation Payload Internal fragmentation p1 = malloc(4) p = malloc(5) Caused by Overhead of maintaining heap data structures Padding foralignment purposes Explicit policy decisions (e.g., to return a big block to satisfy a small request) p3 = malloc(6) free(p) p4 = malloc(6) Oops! (what would happen now?) 11 1 CSC5 - Spring 01 3

Structure of Allocated Block Standard method Keep the length of a block in the word preceding the block. This word is often called the header field or header Requires an extra word for every allocated block p0 Free Block Management How to keep track of free blocks? Allocate from free blocks: How do we pick a block to use for allocation many might fit? What do we do with the extra space when allocating a structure that is smaller than the free block it is placed in? How do we reinsert freed block? p0 = malloc(4) 5 block size data free(p0) 13 14 Keeping Track of Free Blocks Method 1: Implicit list using length links all blocks 5 4 6 Method : Explicit list among the free blocks using pointers 5 4 6 Method 1: Implicit Free Block List For each block we need both size and allocation status Could store this information in two words: wasteful! Standard d tik trick If blocks are aligned, some low order address bits are always 0 Instead of storing an always 0 bit, use it as a allocated/free flag When reading size word, must mask out this bit 1 word Method 3: Segregated free list Different free lists for different size classes Method 4: Blocks sorted by size Can use a balanced tree (e.g. Red Black tree) with pointers within each free block, and the length used as a key 15 Format of allocated and free blocks Size Payload Optional padding a a = 1: Allocated block a = 0: Free block Size: block size Payload: application data (allocated blocks only) 16 CSC5 - Spring 01 4

Detailed Implicit Free List Example Implicit List: Finding a Free Block Start of heap Unused 8/0 16/1 3/0 16/1 Double word aligned Allocated blocks: shaded Free blocks: unshaded Headers: labeled with size in bytes/allocated bit First fit: Search list from beginning, choose first free block that fits: Can take linear time in total number of blocks (allocated and free) It may cause splinters at beginning of list Next fit: Like first fit, but search list starting where previous search finished Should often be faster than first fit: avoids re scanning unhelpful blocks Some research suggests that fragmentation is worse Best fit: Search the list, choose the best free block: fits, with fewest bytes left over Keeps fragments small usually helps fragmentation Will typically run slower than first fit 17 18 Implicit List: Allocating in Free Block Allocating in a free block: splitting Since allocated space might be smaller than free space, we might want to split the block 4 4 6 Implicit List: Freeing a Block Simplest implementation: Need only clear the allocated flag void free_block(ptr p) { *p = *p & - } But can lead to false fragmentation 4 p 4 4 free(p) malloc(5) 4 4 4 4 4 4 Oops! p There is enough free space, but the allocator won t be able to find it 19 0 CSC5 - Spring 01 5

Implicit List: Coalescing Join (coalesce) with next/previous blocks, if they are free Coalescing with next block free(p) 4 4 4 4 4 6 p logically gone Implicit List: Bidirectional Coalescing Boundary tags [Knuth73] Replicate size/allocated word at bottom (end) of free blocks Allows us to traverse the list backwards, but requires extra space Important and general technique! 4 4 4 4 6 6 4 4 But how do we coalesce with previous block? 1 Format of allocated and free blocks Header Boundary tag (footer) Size Payload and padding Size a a a = 1: Allocated block a = 0: Free block Size: Total block size Payload: Application data (allocated blocks only) Constant Time Coalescing Constant Time Coalescing (Case 1) Case 1 Case Case 3 Case 4 Block being freed Allocated Allocated Allocated Free Free Allocated Free Free m 1 n 0 n 0 m 1 m 1 m 1 3 4 CSC5 - Spring 01 6

Constant Time Coalescing (Case ) Constant Time Coalescing (Case 3) m1 0 n+m1 0 m1 0 n+m 0 n+m1 0 m 0 m 1 m 1 m 0 n+m 0 m 1 m 1 5 6 Constant Time Coalescing (Case 4) Space use of footers m1 0 m1 0 n+m1+m 0 Additional space consumption Can it be optimized? Footer tag is free in free blocks No need to coalesce in allocated blocks m 0 m 0 n+m1+m 0 7 8 CSC5 - Spring 01 7

Implicit Free Block Lists: Summary Disclaimer Implementation: very simple Allocate cost: linear time worst case Free cost: constant time worst case even with coalescing Memory usage: Depend on placement policy: first fit, next fit or best fit These slides were adapted from the CMU course slides provided alongwith the textbook of Computer Systems: A programmer s Perspective by Bryant and O Hallaron. The slides are intended for the sole purpose of teaching the computer organization course at the University of Rochester. Not used in general allocator because of linear time allocation Segregated free lists (next lecture) approximate a best fit placement policy without having to search entire free list 9 30 CSC5 - Spring 01 8