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



Similar documents
Chapter 7 Memory Management

OPERATING SYSTEM - MEMORY MANAGEMENT

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

Operating Systems CSE 410, Spring File Management. Stephen Wagner Michigan State University

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

DATA STRUCTURES USING C

Chapter 7 Memory Management

Java's garbage-collected heap

1 File Management. 1.1 Naming. COMP 242 Class Notes Section 6: File Management

1 The Java Virtual Machine

Chapter 12 File Management

Chapter 12 File Management

Chapter 12 File Management. Roadmap

File System Management

CS5460: Operating Systems

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

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

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 13-1

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

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

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

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

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

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

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

Operating Systems, 6 th ed. Test Bank Chapter 7

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.

The Design of the Inferno Virtual Machine. Introduction

Chapter 12. Paging an Virtual Memory Systems

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

Garbage Collection in the Java HotSpot Virtual Machine

Disk Space Management Methods

Physical Data Organization

Part III Storage Management. Chapter 11: File System Implementation

Virtual vs Physical Addresses

Module 2 Stacks and Queues: Abstract Data Types

Persistent Binary Search Trees

Chapter 5 Names, Bindings, Type Checking, and Scopes

Memory management basics (1) Requirements (1) Objectives. Operating Systems Part of E1.9 - Principles of Computers and Software Engineering

File Management Chapters 10, 11, 12

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

Computer Architecture

6. Storage and File Structures

Lecture 1: Data Storage & Index

Memory Management in the Java HotSpot Virtual Machine

COS 318: Operating Systems. File Layout and Directories. Topics. File System Components. Steps to Open A File

Naming vs. Locating Entities

INTRODUCTION The collection of data that makes up a computerized database must be stored physically on some computer storage medium.

10CS35: Data Structures Using C

COS 318: Operating Systems

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

Jonathan Worthington Scarborough Linux User Group

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Introduction to Embedded Systems. Software Update Problem

Simple Solution for a Location Service. Naming vs. Locating Entities. Forwarding Pointers (2) Forwarding Pointers (1)

Record Storage and Primary File Organization

Common Data Structures

Chapter 3 Operating-System Structures

File Management. Chapter 12

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

NetBeans Profiler is an

Chapter 11: File System Implementation. Chapter 11: File System Implementation. Objectives. File-System Structure

The V8 JavaScript Engine

Data Structures. Level 6 C Module Descriptor

HP Service Manager Shared Memory Guide

Chapter 13 File and Database Systems

Chapter 13 File and Database Systems

Memory management. Chapter 4: Memory Management. Memory hierarchy. In an ideal world. Basic memory management. Fixed partitions: multiple programs

KVM & Memory Management Updates

Data Structures and Data Manipulation

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

Recent Advances in Financial Planning and Product Development

OPERATING SYSTEMS MEMORY MANAGEMENT

Chapter 8: Structures for Files. Truong Quynh Chi Spring- 2013

DATABASE DESIGN - 1DL400

Programming Embedded Systems

File Systems Management and Examples

I/O Management. General Computer Architecture. Goals for I/O. Levels of I/O. Naming. I/O Management. COMP755 Advanced Operating Systems 1

PART IV Performance oriented design, Performance testing, Performance tuning & Performance solutions. Outline. Performance oriented design

Page 1 of 5. IS 335: Information Technology in Business Lecture Outline Operating Systems

CHAPTER 17: File Management

Node-Based Structures Linked Lists: Implementation

Introduction to Programming System Design. CSCI 455x (4 Units)

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

Linux VM Infrastructure for memory power management

The Deadlock Problem. Deadlocks. Deadlocks. Bridge Crossing Example

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

Free-Space Management

File-System Implementation

Lecture 25 Symbian OS

Parameter passing in LISP

General Introduction

File Management. Chapter 12

Chapter 11 I/O Management and Disk Scheduling

The Linux Virtual Filesystem

Understanding Java Garbage Collection

B M C S O F T W A R E, I N C. BASIC BEST PRACTICES. Ross Cochran Principal SW Consultant

Data Structures Fibonacci Heaps, Amortized Analysis

Transcription:

Dynamic Storage Allocation CS 44 Operating Systems Fall 5 Presented By Vibha Prasad Memory Allocation Static Allocation (fixed in size) Sometimes we create data structures that are fixed and don t need to grow or shrink. Dynamic Allocation (change in size) At other times, we want to increase and decrease the size of our data structures to accommodate changing needs. Often, real world problems mean that we don t know how much space to declare, as the number needed will change over time. Static Allocation Dynamic Allocation Done at compile time. Global variables variables declared ahead of time, such as fixed arrays. Lifetime = entire runtime of program Advantage efficient execution time. Disadvantage? If we declare more static data space than we need, we waste space. If we declare less static space than we need, we are out of luck. Done at run time. Data structures can grow and shrink to fit changing data requirements. We can allocate (create) additional storage whenever we need them. We can de-allocate (free/delete) dynamic space whenever we are done with them. Advantage we can always have exactly the amount of space required - no more, no less. For example, with references to connect them, we can use dynamic data structures to create a chain of data structures called a linked list. Dynamic Allocation Memory Management Local Variables Lifetime = duration of procedure activation (as long as that method is active) Advantage efficient storage use. Allocation (all variables allocated within the procedure scope unless declared static). For example, parameter passing during function calls. User-allocated variables Lifetime = until the user deletes it (or until it is garbagecollected) Advantage permits creation of dynamic structures like trees, linked lists etc. Heap Allocation. For example, linked lists. The memory of a process is divided into the following parts A space for the code of the program A space for the data (global variables) The stack, for the local variables (static allocation) The heap, for the dynamic variables (dynamic allocation) The division between and is only logical physically the globals and the code are placed at the base of the stack.

Logical Memory Components Memory of a Process Memory area for storing a program (application) and global variables Memory area private to each program for storing local variables Heap A B C Program A Program B Program C Operating System Memory area available to all programs for dynamic data structures Driver Driver Driver Driver Driver Logical View Heap (Dynamic Area) (Dynamic variables go here) (Static Area) (Local variables go here) Data (global variables) Module Code Physical Memory Mapping (Static Area) (Local variables go here) Heap (Dynamic Area) (Dynamic variables go here) Data (global variables) Module Code n Why isn t static allocation sufficient? Recursive procedures Complex data structures If all storage must be reserved in advance (statically), then it will be used inefficiently (enough will be reserved to handle the worst possible case). OS cannot predict how many jobs there will be or which programs will be run or when a process will come and ask for storage space!!!!!! Dynamic Memory Management Dynamic memory allocation is performed by the operating system when the program is executing and the program (user) sends a request for additional memory. Three issues need to be addressed by the operating system allocating variable-length dynamic storage when needed freeing up storage when requested managing the storage (e.g. reclaiming freed up memory for later use) Dynamic Storage Allocation methods Allocation Used to allocate local variables. Grown and shrunk on procedure calls and returns. Register allocation works best for stack-allocated objects. Heap Allocation Used to allocate dynamic objects. Heap objects are accessed with pointers. Never allocated to registers. -Based Allocation Memory allocation and freeing are partially predictable. Restricted but simple and efficient. Allocation is hierarchical Memory freed in opposite order of allocation. If alloc(a) then alloc(b) then alloc(c), then it must be free(c) then free(b) then free(a).

-Based Allocation Example Procedure call Program calls Y, which calls X. Each call pushes another stack frame on top of the stack. Each stack frame has space for variable, parameters, and return addresses. s are also useful for tree traversal, expression evaluation, top-down recursive parsers etc. Local variables Parameters Return address -Based Allocation Example A stack-based organization keeps all the free space together in one place. Frame for X Frame for Y Frame for X Frame for X After call to X after call to Y after exiting Y after exiting X Heap Organization Heap Organization Allocation and release are unpredictable. Heaps are used for arbitrary list structures, complex data organizations. More general, less efficient. Example payroll system. Do not know when employees will join and leave the company. Must be able to keep track of all of them using the least possible amount of storage. Memory consists of allocated areas and free areas (holes). Goal Reuse the spaces in holes. Keep the no. of holes small. Keep the size of holes large. Problem Fragmentation!! Holes may be too small to be useful for any allocation. Inefficient use of memory..... n Fragmentation and Compaction Heap Allocation Methods External fragmentation Total memory space exists to satisfy a request, but it is not contiguous. Internal fragmentation Allocated memory may be slightly larger than requested memory; holes in the memory block allocated. Compaction Reduce external fragmentation. Shuffle memory contents to place all free memory together in one large block. Typically, heap allocation schemes use a free list to keep track of the storage that is not in use. Algorithms differ in how they manage the free list Best Fit Search the whole list on each allocation. Choose block that comes closest to matching the needs of allocation. Save excess for later. First Fit Scan the list for the first block that comes closest to matching the needs of allocation. 3

Example Best Fit Example First Fit 4 5 6 6 6 4 6 6 4 4 6 4 5 6 6 6 4 6 6 4 4 6 4 5 6 6 6 4 6 6 5 4 6 7 5 6 6 6 4 6 6 4 4 6 Other Heap Allocation Methods Worst Fit Search the whole list on each allocation. Choose block that worst matches the request. Save excess for later. Next Fit Start where the last search left off. Scan the list for the first block that comes closest to matching the needs of allocation. Example Worst Fit 4 4 5 6 6 6 5 6 6 6 4 4 6 6 6 6 4 4 4 6 4 6 Example Next Fit Bit Map 4 5 This is where the last search left off 6 6 6 4 6 6 4 5 6 6 6 3 6 6 4 4 4 6 4 6 Bit Map Used when allocation comes in fixed-size chunks (e. g. disk blocks, or 3- byte chunks). Keep a large array of bits, one for every chunk If bit is, chunk is in use. If bit is, chunk is free. No need to merge later operations. Problem Internal Fragmentation!!! Bit Map Memory 4

Pools Reclamation Methods Pools Keep a separate allocation pool for every size. Allocation fast. No fragmentation. Any problems? Inefficiency if some pools run out of space while others have a lot of free space. Solution? Shuffle between pools. 3 64 56 How do we know when dynamically-allocated memory can be freed? Easy when chunk only used in one place. Harder when information is shared (Pointers). Problems Dangling Pointers Memory Leaks Solution Reference Count Garbage Collection Reference Counter Method Reference Counter Example Reference Counts keep track of the number of outstanding pointers for each chunk of memory. The reference counts must be managed automatically (by the system) so no mistakes are made in incrementing and decrementing them. Each time a new pointer refers to the block of memory, the reference count of the block must be increased by. Each time a pointer that refers the block of memory is changed so it no longer points to that sublist, the reference count of the block must be decreased by. When reference count becomes zero, free the memory. Example File descriptors in Unix. Process Process Process 3 Process 4 Memory A B 3 C Process 5 Reference Counts Process 6 Reference Counter Example Reference Counter Example Process Process Process 3 Process 4 Memory A B C 3 Process 5 Reference Counts Process 6 Process 6 does not need block C anymore Process Process Process 3 Process 4 Memory A B C Process 5 Reference Counts Reference Count Updated!!! 5

Reference Counter Method Garbage Collection Advantages Unused blocks of memory are returned to the available list as soon as they are unused. Time spent for reclamation is being undertaken continually (unlike garbage collection which happens irregularly and takes more execution time when it does happen). Disadvantages Requires additional overheads (storage of the reference count for each block, reference count updating algorithm etc. Is still time consuming for reference count updating etc.. (but more dynamic than garbage collection). At all times, the heap may contain three kinds of dynamic data structures (or memory blocks) Blocks that are in use. Blocks that are not in use and are on the current list of available memory space. Blocks that are not in use but are not on the list of available memory space (garbage). The management (reclaiming and recycling) of garbage nodes is called garbage collection. Garbage Collection Garbage Collection Blocks in use Blocks not in use and on the list of available space Blocks not in use but are not on the list of available space (garbage) Blocks of type and 3 should be merged together!!!! Garbage Collection No explicit free operation by the user. When system needs storage, it searches through all the pointers and collects things that aren t used. Only way in circular structures. Incredibly difficult to program and debug. Must be able to find all pointers to objects. Must be able to find all objects. Garbage Collection Mechanism Garbage Collection Example Initialization Initialize all memory blocks to be free. Pass Mark Go through all statically-allocated and procedure-level variables, looking for pointers. Mark each object pointed to, and recursively mark all objects it points to. Pass Sweep Go through all objects, free up those that aren t marked. Problem Expensive!!! Initial State of Memory Memory after Marking Memory after Garbage Collection Allocated Marked Freed 6

Questions? 7