Memory Management III Memory Alloca0on

Similar documents
File Systems I COMS W4118

CIS Operating Systems Memory Management. Professor Qiang Zeng Fall 2017

Operating Systems. 11. Memory Management Part 3 Kernel Memory Allocation. Paul Krzyzanowski Rutgers University Spring 2015

Memory Management. COMP755 Advanced Operating Systems

CIS Operating Systems Contiguous Memory Allocation. Professor Qiang Zeng Spring 2018

CIS Operating Systems Memory Management Address Translation. Professor Qiang Zeng Fall 2017

Requirements, Partitioning, paging, and segmentation

Alterna(ve Architectures

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

Requirements, Partitioning, paging, and segmentation

Operating systems. Part 1. Module 11 Main memory introduction. Tami Sorgente 1

! What is main memory? ! What is static and dynamic allocation? ! What is segmentation? Maria Hybinette, UGA. High Address (0x7fffffff) !

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

Memory Allocators. Pradipta De

File Systems II. COMS W4118 Prof. Kaustubh R. Joshi hdp://

Events, Memory Management

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

Dynamic Memory Alloca/on: Basic Concepts

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

CS61C : Machine Structures

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

The Art and Science of Memory Alloca4on

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

Introduction to Operating Systems

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

In multiprogramming systems, processes share a common store. Processes need space for:

hashfs Applying Hashing to Op2mize File Systems for Small File Reads

Administrivia. Project 2 due Friday at noon Midterm Monday. Midterm review section Friday Section for Project 3 next Friday

CS61C : Machine Structures

Administrivia. Dynamic memory allocation. More abstractly. Why is it hard? What is fragmentation really? Important decisions

Run-time Environments - 3

Administrivia. Project 2 due Friday at 12pm (noon) Midterm Monday 2/12

Run-time Environments -Part 3

Lecture 2: Memory in C

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

Memory Management Basics

CS61C : Machine Structures

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

6. Which of the following operating systems was the predecessor of Microsoft MS-DOS? a. Dynix b. Minix c. CP/M

Administrivia. - Open book, open notes (but not open notebook computer) - Covers first 10 lectures of course (including today)

CS61C : Machine Structures

Dynamic Memory Alloca/on: Basic Concepts

Memory Management william stallings, maurizio pizzonia - sistemi operativi

Preview. Memory Management

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Processes, Address Spaces, and Memory Management. CS449 Spring 2016

Lectures 13 & 14. memory management

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

ECE 598 Advanced Operating Systems Lecture 10

Memory management. Johan Montelius KTH

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

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

Operating System Labs. Yuanbin Wu

Opera&ng Systems CMPSCI 377 Dynamic Memory Management. Emery Berger and Mark Corner University of Massachuse9s Amherst

1. With respect to space, linked lists are and arrays are.

Basic Memory Management

Basic Memory Management. Basic Memory Management. Address Binding. Running a user program. Operating Systems 10/14/2018 CSC 256/456 1

CS Operating Systems

CS Operating Systems

CSCI 4500 / 8506 Sample Questions for Quiz 5

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 13: Address Translation

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

CS61C : Machine Structures

Virtual Memory Management

Process. One or more threads of execution Resources required for execution. Memory (RAM) Others

Memory Management. Before We Begin. Process s Memory Address Space. Process Memory. CSE 120: Principles of Operating Systems.

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Status of the Linux Slab Allocators

COMPUTER SCIENCE 4500 OPERATING SYSTEMS

File Systems. COMS W4118 Prof. Kaustubh R. Joshi hcp://

Synchroniza+on II COMS W4118

RAM, Design Space, Examples. January Winter Term 2008/09 Gerd Liefländer Universität Karlsruhe (TH), System Architecture Group

Dynamic Memory Allocation

Kernel Memory Management

File System Internals. Jo, Heeseung

Part II: Memory Management. Chapter 7: Physical Memory Chapter 8: Virtual Memory Chapter 9: Sharing Data and Code in Main Memory

Process. Memory Management

Process. One or more threads of execution Resources required for execution. Memory (RAM) Others

Virtual Memory B: Objec5ves

Operating Systems. Week 9 Recitation: Exam 2 Preview Review of Exam 2, Spring Paul Krzyzanowski. Rutgers University.

Programming Languages

Virtual Memory Outline

Operating Systems. Memory Management. Lecture 9 Michael O Boyle

Process. One or more threads of execution Resources required for execution

Memory Management. Dr. Yingwu Zhu

Binghamton University Dynamic Memory Alloca/on

Memory Management. Memory Management

ECE 598 Advanced Operating Systems Lecture 12

The Memory Management Unit. Operating Systems. Autumn CS4023

Engine Support System. asyrani.com

Chapter 10: Case Studies. So what happens in a real operating system?

CS420: Operating Systems

CS61C Midterm Review on C & Memory Management

Disks: Structure and Scheduling

Chapter 3 - Memory Management

DYNAMIC MEMORY ALLOCATOR ALGORITHMS SIMULATION AND PERFORMANCE ANALYSIS

Memory Management. Memory Management

Main Points. File layout Directory layout

Introduction to Paging

Transcription:

Memory Management III Memory Alloca0on COMS W4118 Prof. Kaustubh R. Joshi krj@cs.columbia.edu hgp://www.cs.columbia.edu/~krj/os References: Opera0ng Systems Concepts (9e), Linux Kernel Development, previous W4118s Copyright no2ce: care has been taken to use only those web images deemed by the instructor to be in the public domain. If you see a copyrighted image on any slide and are the copyright owner, please contact the instructor. It will be removed. 1

Outline Dynamic memory alloca0on overview Heap alloca0on strategies 2

Dynamic memory alloca0on Paging solves con0guous memory problem Virtual memory is con0guous Pages can be discon0guous But, paging doesn t always work for kernel memory Requests smaller than a page (e.g., kmalloc) DMA hardware doesn t understand paging unless IOMMU support is available Two ways of dynamic alloca0on Stack alloca0on Restricted, but simple and efficient Heap alloca0on More general, but less efficient More difficult to implement 3

Dynamic alloca0on issue: fragmenta0on Fragment: small chunk of free memory, too small for future alloca0on requests ( holes ) External fragment: visible to alloca0on system Internal fragment: visible to process (e.g. if allocate at some granularity) Goal Reduce number of holes Keep holes large Stack fragmenta0on v.s. heap fragmenta0on Stack: all free space is one big hole no fragmenta0on Can only deallocate when everything above you is gone Heap: fragmenta0on possible 4

Typical heap implementa0on Data structure: free list Chains free blocks together Alloca0on Choose block large enough for request Update free list Free Add block back to list Merge adjacent free blocks (reduce fragmenta0on) 5

Heap alloca0on strategies Best fit Search the whole list on each alloca0on Choose the smallest block that can sa0sfy request Can stop search if exact match found First fit Choose first block that can sa0sfy request Worst fit Choose largest block (most lehover space) Which is beger? 6

Example Free space: 2 blocks, size 20 and 15 Workload 1: alloca0on requests: 10 then 20 Best fit First fit Worst fit Request of 20: fail! Request of 20: fail! Workload 2: alloca0on requests: 8, 12, then 13 Best fit First fit Request of 13: fail! Worst fit Request of 13: fail! 7

Comparison of alloca0on strategies Best fit Tends to leave very large holes and very small holes Disadvantage: very small holes may be useless First fit: Tends to leave average size holes Advantage: faster than best fit Worst fit: Simula0on shows that worst fit is worst in terms of storage u0liza0on 8

Buddy allocator mo0va0on Alloca0on requests: frequently 2 n E.g., alloca0on physical pages in FreeBSD and Linux Generic alloca0on strategies: overly generic Fast search (allocate) and merge (free) Avoid itera0ng through en0re free list Avoid external fragmenta0on for req of 2 n ; keep free pages con0guous Real: used in FreeBSD and Linux 9

Buddy allocator implementa0on Alloca0on restric0ons: 2 k, 0<= k <= N Data structure N free lists of blocks of size 2 0, 2 1,, 2 N Alloca0on of 2 k : Search free lists (k, k+1, k+2, ) for appropriate size Free Recursively divide larger blocks un0l reach block of correct size Insert buddy blocks into free lists Recursively coalesce block with buddy if buddy free 10

Buddy System Allocator 11

Buddy alloca0on example p1 = alloc(2^0) p2 = alloc(2^2) freelist[3] = {0} that color. freelist[0] = {1}, freelist[1] = {2} freelist[2] = {4} freelist[0] = {1}, freelist[1] = {2} Color Legend: Black: allocated. Other: on freelist of freelist[3] = free list for blocks of 2^3 pages. free(p1) freelist[2] = {0} free(p2) freelist[3] = {0} 12

Pros and cons of buddy allocator Advantages Fast and simple compared to general dynamic memory alloca0on Avoid external fragmenta0on by keeping free physical pages con0guous Disadvantages Internal fragmenta0on Alloca0on of block of k pages when k!= 2^n 13

Slab allocator Mo0va0on: Frequent (de)alloca0on of some kernel objects, E.g., task_struct, inode Other allocators: overly general; assume variable size Cache: slab of slots Each cache holds only single object type (task_struct, inode, dentry, vma) Each cache has one (or more) slabs, each 1 page long Each slab is split into slots Slot size = object size Slab opera0ons Free memory management = bitmap Allocate: set bit and return slot Free: clear bit Used in FreeBSD and Linux on top of buddy page allocator For objects smaller than a page kmem_cache_create: create a new cache for your own object type kmem_cache_alloc: allocate new object from cache 14

Slab Allocation 15