Memory Management -Memory allocation -Garbage collection.

Slides:



Advertisements
Similar presentations
Chapter 6: Memory Management
Advertisements

Kernel memory allocation
Chapter 12. Kernel Memory Allocation
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
KERNEL MEMORY ALLOCATION Unix Internals, Uresh Vahalia Sowmya Ponugoti CMSC 691X.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
7. Physical Memory 7.1 Preparing a Program for Execution
CPSC 388 – Compiler Design and Construction
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
CS 61C L07 More Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
File System Implementation
Memory Management Chapter 7. Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated efficiently to pack as.
Dynamic memory allocation and fragmentation Seminar on Network and Operating Systems Group II.
Memory Management Memory Areas and their use Memory Manager Tasks:
1 Storage Allocation Operating System Hebrew University Spring 2007.
Memory Management A memory manager should take care of allocating memory when needed by programs release memory that is no longer used to the heap. Memory.
Linked lists and memory allocation Prof. Noah Snavely CS1114
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
4.0 : File Management.
Memory Allocation CS Introduction to Operating Systems.
The memory allocation problem Define the memory allocation problem Memory organization and memory allocation schemes.
Memory management. Instruction execution cycle Fetch instruction from main memory Decode instruction Fetch operands (if needed0 Execute instruction Store.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
Dynamic Partition Allocation Allocate memory depending on requirements Partitions adjust depending on memory size Requires relocatable code –Works best.
GC Algorithm inside.NET Luo Bingqiao 5/22/2009. Agenda 1. 经典基本垃圾回收算法 2.CLR 中垃圾回收算法介绍 3.SSCLI 中 Garbage Collection 源码分析.
External Storage Primary Storage : Main Memory (RAM). Secondary Storage: Peripheral Devices –Disk Drives –Tape Drives Secondary storage is CHEAP. Secondary.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Memory Management Problem: Records (of various lengths) need to be stored. Model: A big array of space to store them, managed by a memory manager. Like.
CS 241 Discussion Section (11/17/2011). Outline Review of MP7 MP8 Overview Simple Code Examples (Bad before the Good) Theory behind MP8.
PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Heap storage Dynamic allocation and stacks are generally.
Heap storage & Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Memory Management Overview.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Mono an multiprogramming.
Consider Starting with 160 k of memory do: Starting with 160 k of memory do: Allocate p1 (50 k) Allocate p1 (50 k) Allocate p2 (30 k) Allocate p2 (30 k)
CS 241 Discussion Section (2/9/2012). MP2 continued Implement malloc, free, calloc and realloc Reuse free memory – Sequential fit – Segregated fit.
CS 241 Discussion Section (12/1/2011). Tradeoffs When do you: – Expand Increase total memory usage – Split Make smaller chunks (avoid internal fragmentation)
CS61C L07 More Memory Management (1) Garcia, Spring 2010 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Part III Storage Management
External fragmentation in a paging system Use paging circuitry to map groups of noncontiguous free pages into logically contiguous addresses (remap your.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Memory Management Chapter 7.
Chapter 17 Free-Space Management
Storage Allocation Mechanisms
Garbage Collection What is garbage and how can we deal with it?
Memory Management Memory Areas and their use Memory Manager Tasks:
Memory management.
CS 326 Programming Languages, Concepts and Implementation
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Partitioned Memory Allocation
Memory Management 6/20/ :27 PM
Dynamic Memory Allocation
26 - File Systems.
Concepts of programming languages
Memory Management Memory Areas and their use Memory Manager Tasks:
Module IV Memory Organization.
Optimizing Malloc and Free
CS Introduction to Operating Systems
Memory Management Overview
Chapter 12 Memory Management
Memory Management (1).
Memory Management Memory Areas and their use Memory Manager Tasks:
CS703 - Advanced Operating Systems
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Memory Management -Memory allocation -Garbage collection

Memory Allocation Memory pool: large block of contiguous memory Memory manager allocates memory by returning a handle to the user Use the term heap to refer to free memory accessed by a dynamic memory management scheme

Dynamic Allocation Blocks of any size may be requested in any order from the free-list For a request of m words, and a block size k, between m and k space is used for the request This can result in fragmentation if m != k

Fragmentation in Dynamic Allocation External fragmentation: lots of small free blocks Internal fragmentation: when all of block of size k is allocated for m words. This type of allocation is easier, if less efficient

Sequential Fit Method Attempt to find a “good” block The free-list is organized as a doubly- linked list Tag bit and block size fields The memory manager searches the free- list for a block of “suitable” size

Three sequential fit methods First fit –Start from the beginning (or middle) –May waste larger blocks by breaking them up Best fit –Examines entire list –Maximizes external fragmentation but will be more likely to be able to service large requests

Three sequential fit methods Worst fit –Allocates largest block through a sequential search –Minimizes external fragmentation Which is best? Depends on the expected types of memory requests

Sequential Fit A search of the free-list is in Ө(n) in the worst case Want to merge adjacent free blocks Need additional space to support the memory manager operations/linked list Is there anything that can be improved?

The Buddy Method Assume that memory is of size 2ⁿ for some n Both free and reserved blocks will be of size 2 k for k ≤ n The buddy system keeps a separate list of free blocks for each size

The Buddy Method For a request of size m, find the smallest k where 2 k ≥ m If such a k exists, allocate a block of size 2 k from the list If such a k does not exist, allocate the next larger block on the list, and split it in half until a block of size 2 k is created

The Buddy Method Advantages –Less external fragmentation –Cheaper search than a linked list –Merging adjacent blocks is easy (the buddy for any block of size 2 k is another block of the same size with the same address except the k th bit is reversed) Disadvantage –Allows internal fragmentation

Other memory allocation methods Segregated storage method: break available memory into several memory zones, each with its own management method Impose a standard size (cluster scheme) –Example: disk file management –Leads to internal fragmentation –Does not need to be contiguous

Failure Policies Happens when a memory request for a certain size cannot be serviced –Due to external fragmentation → compact memory, which physically moves data –Use a handle if the application relies on absolute positions of the data –Can defer the memory request (for example, when several processes are running at once)

Failure policies: Garbage Collection When no program variable points to a block of space, it is considered garbage (or a memory leak) Garbage collection involves determining which memory is garbage and recovering it Two common methods: –Reference count –Mark/sweep strategy

Garbage Collection Reference Count: each dynamically allocated memory block has a count field that is incremented and decremented for each pointer pointing to it (or away from it) –When the count reaches zero, the memory becomes garbage and is immediately placed in free store –Used by the UNIX file system, where the memory objects are linked together without cycles –Useful when the objects are large, such as a file

Garbage Collection Mark/sweep strategy –Uses a single bit marker instead of a count field –Works for cycles, but DFS is recursive Garbage collection phase occurs when free store is exhausted: –Clear all mark bits –Perform DFS from each pointer on the variable list, turning on the bits –Sweep through the memory pool for unmarked elements (which are garbage and placed in free store)