Dynamic Memory: Alignment and Fragmentation

Similar documents
Dynamic Data Structures. CSCI 112: Programming in C

A First Book of ANSI C

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

Dynamic Memory Allocation. CS 351: Systems Programming Michael Saelee

Run-time Environments

Run-time Environments

CS201 Some Important Definitions

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

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Data Representation and Storage. Some definitions (in C)

Heap Arrays. Steven R. Bagley

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

CS 61C: Great Ideas in Computer Architecture C Memory Management, Usage Models

Operating System Labs. Yuanbin Wu

For Teacher's Use Only Q No Total Q No Q No

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

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

Short Notes of CS201

Pointers, Dynamic Data, and Reference Types

Fall 2018 Discussion 2: September 3, 2018

CS201 - Introduction to Programming Glossary By

Memory Management: The process by which memory is shared, allocated, and released. Not applicable to cache memory.

CS 11 C track: lecture 5

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

Advanced Programming & C++ Language

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

Pointers II. Class 31

ECE 598 Advanced Operating Systems Lecture 10

Data Structure Series

Programs in memory. The layout of memory is roughly:

Lecture 8 Dynamic Memory Allocation

Dynamic Memory Allocation. Zhaoguo Wang

Section 2: Processes

CS Week 8 Lab Assignment 3. Teaching Assistant Henrique Potter

DAY 3. CS3600, Northeastern University. Alan Mislove

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

CSC 1600 Memory Layout for Unix Processes"

Memory (Stack and Heap)

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

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

C++ for Java Programmers

Low-Level C Programming. Memory map Pointers Arrays Structures

Recitation #11 Malloc Lab. November 7th, 2017

Come and join us at WebLyceum

Lectures 13 & 14. memory management

CSCE Operating Systems Non-contiguous Memory Allocation. Qiang Zeng, Ph.D. Fall 2018

10.1. CS356 Unit 10. Memory Allocation & Heap Management

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

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

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

Kernel Memory Management

CP2 Revision. theme: dynamic datatypes & data structures

Memory management. Johan Montelius KTH

Dynamic Memory Management! Goals of this Lecture!

CS61C : Machine Structures

COMPUTER SCIENCE 4500 OPERATING SYSTEMS

Memory Allocation. General Questions

Secure Software Programming and Vulnerability Analysis

LANGUAGE RUNTIME NON-VOLATILE RAM AWARE SWAPPING

Lab 1: Dynamic Memory: Heap Manager

Class Information ANNOUCEMENTS

CS Operating Systems

CS Operating Systems

Homework 3 CS161 Computer Security, Fall 2008 Assigned 10/07/08 Due 10/13/08

Dynamic Memory Allocation

1 Dynamic Memory continued: Memory Leaks

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

The C++ Object Lifecycle. EECS 211 Winter 2019

CS201 Latest Solved MCQs

Base Component. Chapter 1. *Memory Management. Memory management Errors Exception Handling Messages Debug code Options Basic data types Multithreading

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Dynamic Memory CMSC 104 Spring 2014, Section 02, Lecture 24 Jason Tang

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Engine Support System. asyrani.com

CS 322 Operating Systems Practice Midterm Questions

Assignment 5. CS/ECE 354 Spring 2016 DUE: April 22nd (Friday) at 9 am

Section - Computer Science. int main() {! int a=10,b=20;! printf("a:%d B:%d\n",a,b);! a=(a+b)-(b=a);! printf("a:%d B:%d\n",a,b);!

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

CS201- Introduction to Programming Current Quizzes

Jump Tables A jump table is essentially a list of places in the code to jump to. This can be thought of in C as an array of function pointers. In Asse

High Performance Computing and Programming, Lecture 3

Cpt S 122 Data Structures. Data Structures

Reminder: compiling & linking

CS61C : Machine Structures

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

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

Limitations of the stack

CS558 Programming Languages

Abstractions and Reality

Common Misunderstandings from Exam 1 Material

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

Recall: Address Space Map. 13: Memory Management. Let s be reasonable. Processes Address Space. Send it to disk. Freeing up System Memory

Section Notes - Week 1 (9/17)

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

6.S096 Lecture 2 Subtleties of C

CS399 New Beginnings. Jonathan Walpole

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Dynamic Memory Management

Naming in OOLs and Storage Layout Comp 412

Transcription:

Dynamic Memory: Alignment and Fragmentation Learning Objectives Explain the purpose of dynamic memory Define the terms arena, heap Identify common errors involving dynamic memory Explain how dynamic memory allocations are aligned Explain internal and external fragmentation Complete assignment 1! 9/15/2015 CS61 Fall 2015 1

Different types of memory In C, you can allocate variables/memory in several different ways: Globals: Accessible from anywhere in your program; typically allocated in a read/write data segment of your process. Global const: Global variables whose values cannot change; typically allocated in a read-only data segment. Locals (regular): Variables private to a function; cannot be accessed by other functions; allocated on the stack (inside the function s call frame). Local static: Like regular locals, they are private to a function, but they retain their value between invocations of the function. Since they cannot go on the stack, they go into data sections (bss if unitialized; read/write data if initialized). Dynamic: Accessible to any code that has a reference to them. Allocated from the heap. The program is responsible for explicitly allocating and deallocating them. 9/15/2015 CS61 Fall 2015 2

Recall: Kernel virtual memory User Stack Memory mapped region for shared libraries Heap Read/write data Read-only code and data The Heap The heap is the region of the program s memory used for dynamic allocation. The heap can grow (up to some limit) to accommodate user requests. The heap expands by calling the sbrk system call. 9/15/2015 CS61 Fall 2015 3

When should you use the heap? Variable sizes are unknown at compile time. The data stored may grow/shrink over the course of the program. The lifetime of the variable is greater than a single function, but it is not truly global. 9/15/2015 CS61 Fall 2015 4

Malloc and Friends I m assuming you are familiar with malloc, free, calloc, realloc, but did you ever notice this (from the man page): The allocated memory is aligned such that it can be used for any data type. What does that mean exactly? Each C object has an associated alignment that indicates at which addresses the variable can be placed. E.g., an alignment of 2 means that the object can be placed at even addresses but not odd addresses. Every type, T, has an alignment, A, such that every object of type T has addresses that are a multiple of A. Since malloc doesn t know what will be stored in the allocated memory, it must always ensure that it provides memory capable of holding data of any type. 9/15/2015 CS61 Fall 2015 5

9/15/2015 CS61 Fall 2015 6

Alignment of structures In class, we asked you to experiment with different arrangements of data types within structures and derive some rules about how structures are aligned. You should have discovered the following (T is a type): sizeof(t) is a multiple of alignof(t). Alignof(struct T) = max{alignof{f} for each field F in T} A struct s size and alignment have to be such that you can allocate structures in an array. The compiler will pad structures as necessary (add unused bytes to make field alignments work). 9/15/2015 CS61 Fall 2015 7

struct s{ char c1; short s; } char c2; int i; 9/15/2015 CS61 Fall 2015 8

struct s{ char c1; short s; } char c2; int i; 9/15/2015 CS61 Fall 2015 9

Fragmentation Consider the following: while {heap space left} { } malloc(256) malloc(128) free all 128-byte objects What will happen when we call malloc(256)? 9/15/2015 CS61 Fall 2015 10

External Fragmentation When we have enough free space to allocate something, but we are unable to do so, due to how that free space is arranged. Can only happen when we permit variable-sized allocations. How do we avoid external fragmentation? 9/15/2015 CS61 Fall 2015 11

Fixed-Size and Slab Allocators If external fragmentation only happens when you allow variable-sized allocations, why not give only fixed size allocations? Slab allocators leverage this idea: Allocate large chunks of memory to different slabs Perform fixed-sized allocation within each slab 9/15/2015 CS61 Fall 2015 12

Internal Fragmentation Arises in fixed size allocators, or allocators that limit the exact sizes in which they will allocate objects. Refers to the space that is inside of an allocation, but unused by the requester. 9/15/2015 CS61 Fall 2015 13

Wrapping Up Dynamic memory allocation is a critical component of many applications. Allocators are required to provide aligned storage. Different types of allocators can produce different types of fragmentation: External fragmentation: free space that you can t use because you don t have consecutive memory available. Internal fragmentation: space that is allocated to an object but unused. 9/15/2015 CS61 Fall 2015 14