Memory Management. Memory Areas and their use. Memory Manager Tasks: Free List Implementations. acquire release

Similar documents
System Software Prof. Dr. H. Mössenböck

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Data Structure [Question Bank]

Analysis of a Search Algorithm

recursion, O(n), linked lists 6/14

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Chapter 7 Memory Management

Persistent Binary Search Trees

Unit Storage Structures 1. Storage Structures. Unit 4.3

DATA STRUCTURES USING C

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Node-Based Structures Linked Lists: Implementation

Data Structures and Data Manipulation

Segmentation and Fragmentation

Introduction to Data Structures and Algorithms

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

OPERATING SYSTEM - MEMORY MANAGEMENT

Sequential Data Structures

Two Parts. Filesystem Interface. Filesystem design. Interface the user sees. Implementing the interface

Introduction to Object-Oriented Programming

CS5460: Operating Systems

10CS35: Data Structures Using C

Chapter 7 Memory Management

Part III Storage Management. Chapter 11: File System Implementation

Introduction to Data Structures

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Lecture 12 Doubly Linked Lists (with Recursion)

Universidad Carlos III de Madrid

LINKED DATA STRUCTURES

APP INVENTOR. Test Review

Linked Lists: Implementation Sequences in the C++ STL

Data Structures Fibonacci Heaps, Amortized Analysis

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Memory Allocation Technique for Segregated Free List Based on Genetic Algorithm

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Trace-Based and Sample-Based Profiling in Rational Application Developer

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Data Structures and Algorithms Lists

Data Structures and Algorithms!

File System Management

Chapter 3: Restricted Structures Page 1

PES Institute of Technology-BSC QUESTION BANK

Algorithms and Data Structures

Internet Working 5 th lecture. Chair of Communication Systems Department of Applied Sciences University of Freiburg 2004

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Data Structures. Level 6 C Module Descriptor

Dynamic Memory Management for Embedded Real-Time Systems

Memory Management Outline. Background Swapping Contiguous Memory Allocation Paging Segmentation Segmented Paging

Chapter 13 Disk Storage, Basic File Structures, and Hashing.

風 水. Heap Feng Shui in JavaScript. Alexander Sotirov.

Binary Search Trees CMPSC 122

Unordered Linked Lists

The Advantages and Disadvantages of Network Computing Nodes

ADTs,, Arrays, Linked Lists

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Chapter 13. Disk Storage, Basic File Structures, and Hashing

Sequences in the C++ STL

Physical Data Organization

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

DATABASE DESIGN - 1DL400

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Data Structures and Algorithms Written Examination

& Data Processing 2. Exercise 3: Memory Management. Dipl.-Ing. Bogdan Marin. Universität Duisburg-Essen

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Free-Space Management

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Data Structures and Algorithms!

EFFICIENT EXTERNAL SORTING ON FLASH MEMORY EMBEDDED DEVICES

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Garbage Collection in the Java HotSpot Virtual Machine

Sample Questions Csci 1112 A. Bellaachia

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

A Practical Method to Diagnose Memory Leaks in Java Application Alan Yu

Analysis of Compression Algorithms for Program Data

Chapter 13 File and Database Systems

Chapter 13 File and Database Systems

West Virginia University College of Engineering and Mineral Resources. Computer Engineering 313 Spring 2010

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

Symbol Tables. Introduction

Segmentation Segmentation: Generalized Base/Bounds

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Java's garbage-collected heap

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Linux VM Infrastructure for memory power management

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Transcription:

Memory Management Memory Areas and their use Memory Manager Tasks: acquire release Free List Implementations Singly Linked List Doubly Linked List Buddy Systems

Memory Management Memory areas: In languages like C or Java, the memory used by a program can be allocated from three different areas: Static: laid out at compilation time, and allocated when the program starts. Used for Global variables and constants Stack: memory is allocated and freed dynamically, in LIFO order. Used for Local variables and parameters Heap: memory is allocated and freed dynamically, in any order. Used for data outliving the method which created them. In Java all objects are stored in the heap The memory management techniques we discuss in this lecture apply exclusively to the management of the heap.

Memory Manager The memory manager is part of the Operating System. It must keep track of which parts of the heap are free, and which are allocated. A memory manager supports the following operations: acquire: allocates memory needed by programs release: deallocates memory no longer needed by programs It also defragments memory when needed

Problems faced in memory allocation Memory fragmentation: External fragmentation: Memory wasted outside allocated blocks Internal fragmentation: Memory wasted inside allocated block. Results when memory allocated is larger than memory requested. Overhead: Additional memory that must be allocated, above and beyond that requested by programs, in order to provide for the management of the heap.

Free List Memory manager uses a free list data structure that keeps track of free memory blocks in a scheme for dynamic memory allocation. Common implementations for free list: Singly-linked list Doubly-linked list Buddy systems: an array of doubly-linked lists Allocation Policies: First fit chooses the first block in the free list big enough to satisfy the request, and split it. Next fit is like first fit, except that the search for a fitting block will start where the last one stopped, instead of at the beginning of the free list. Best fit chooses the smallest block bigger than the requested one. Worst fit chooses the biggest, with the aim of avoiding the creation of too many small fragments but doesn t work well in practice.

Singly-linked list implementation of free-list Each node represents a free block of memory Nodes must be sorted according to start addresses of free blocks so that adjacent free memory blocks can be combined. acquire( ) and release( ) operations are O(n); where n is the number of blocks in the heap. In order to acquire a block, a node is searched following one of the allocation policy. If the block is bigger than requested, it is divided into two. One part is allocated and one remains in the list. In order to release a block, a new node must be inserted (if the adjacent block is not on the free list) or a node, which contains the adjacent free block, must be modified. Searching for the place of the new or existing node has complexity O(n).

Doubly-linked list implementation of free-list In this implementation Nodes are not sorted according to start addresses of free blocks. All memory blocks have boundary tags between them. The tag has information about the size and status (allocated/free) Each node in the doubly linked list represents a free block. It keeps size & start address of the free block and start addresses & sizes of the previous and next memory blocks. The adjacent blocks may be or may not be free The release operation does not combine adjacent free blocks. It simply prepends a node corresponding to a released block at the front of the free list. This operation is thus O(1). Adjacent free blocks are combined by acquire(). The acquire operation traverses the free list in order to find a free area of a suitable size. As it does so it also combines adjacent free blocks.

Node structure: Doubly Linked List Example Initial state of memory (shaded=allocated, grayed=boundary tags) The corresponding free list

Doubly Linked List Example (Cont.) The operation release(400, 4000) will result in: The node corresponding to the freed block is appended at the front of the free-list. The nodes x, y, and z correspond to the three free blocks that have not yet been combined.

Doubly Linked List Example (Cont.) The operation acquire(600) using the first-fit allocation policy will first result in the combination of the three adjacent free blocks: At this point the corresponding free list is:

Doubly Linked List Example (Cont.) The required 600 bytes are then allocated, resulting in: The corresponding free list is:

Buddy Systems implementation of free-list Instead of having a single free list, it has an array of free lists; each element of the array holding blocks of the same size. One type of buddy systems is the binary buddy system. For a memory of size m, there are free-lists of size 2 0, 2 1, 2 2,..., 2 k, where m 2 k The heap is viewed as one large block which can be split into two equal smaller blocks, called buddies. Each of these smaller blocks can again be split into two equal smaller buddies, and so on. Each memory block has its buddy. The buddy of a block of size 2 k that starts at address x is the block of size 2 k that start at address y = complementbit_k(x), where the address bits are numbered from right to left starting with 0.

Buddies If each block is of size 8 bytes (i.e., 2 3 bytes); then the buddy of a block is obtained by complementing bit 3 of its starting address. If each block is of size 4 bytes (i.e., 2 2 bytes); then the buddy of a block is obtained by complementing bit 2 of its starting address. Example: What is the starting address of the buddy of a block that starts at address 1100101010101101 if each block is 16 bytes? Solution: 16 = 2 4 ; the starting address of the buddy is obtained by complementing bit 4: 1100101010111101

Binary Buddy System implementation of free-list Each array element is a list of free blocks of same size. The size of each block is a power of 2.

Binary Buddy System Algorithms acquire(x): x <= 2 k, the corresponding free list is searched If there is a block in this list, it is allocated; otherwise a block of size 2 k+1, 2 k+2, and so on is searched and taken off the free list. The block is divided into two buddies. One buddy is put on the free list for the next lower size and the other is either allocated or further splinted if needed. release(x): The block is placed back in the free list of its size, and if its buddy is also free they are combined to form a free block of size 2 k+1. This block is then moved to the corresponding free list. If its buddy is free they are combined to form a free block of size 2 k+2, which is then moved to the appropriate free list and so on.

Buddy Systems Advantages/Disadvantages Advantage: Both acquire( ) and release( ) operations are fast. Disadvantages: Only memory of size that is a power of 2 can be allocated internal fragmentation if memory request is not a power of 2. When a block is released, its buddy may not be free, resulting in external fragmentation.