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.

Slides:



Advertisements
Similar presentations
CHP-5 LinkedList.
Advertisements

The Linux Kernel: Memory Management
Kernel memory allocation
Understanding Operating Systems Fifth Edition
ICS220 – Data Structures and Algorithms Lecture 13 Dr. Ken Cosh.
KERNEL MEMORY ALLOCATION Unix Internals, Uresh Vahalia Sowmya Ponugoti CMSC 691X.
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Memory Management Chapter 7. Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated efficiently to pack as.
7. Physical Memory 7.1 Preparing a Program for Execution
OS Fall’02 Memory Management Operating Systems Fall 2002.
Chapter 7 Memory Management
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:
Chapter 3.1 : Memory Management
1 Optimizing Malloc and Free Professor Jennifer Rexford
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 Chapter 5.
Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5.
1 Chapter 3.1 : Memory Management Storage hierarchy Storage hierarchy Important memory terms Important memory terms Earlier memory allocation schemes Earlier.
Memory Management Last Update: July 31, 2014 Memory Management1.
Memory Allocation CS Introduction to Operating Systems.
The memory allocation problem Define the memory allocation problem Memory organization and memory allocation schemes.
Real-Time Concepts for Embedded Systems Author: Qing Li with Caroline Yao ISBN: CMPBooks.
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
Memory Management Chapter 7.
Dynamic Partition Allocation Allocate memory depending on requirements Partitions adjust depending on memory size Requires relocatable code –Works best.
Chapter 7 Memory Management
CS 149: Operating Systems March 3 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
1 Advanced Memory Management Techniques  static vs. dynamic kernel memory allocation  resource map allocation  power-of-two free list allocation  buddy.
Chapter 17 Free-Space Management Chien-Chung Shen CIS, UD
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
CS 241 Discussion Section (11/17/2011). Outline Review of MP7 MP8 Overview Simple Code Examples (Bad before the Good) Theory behind MP8.
CS 241 Section Week #9 (11/05/09). Topics MP6 Overview Memory Management Virtual Memory Page Tables.
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 -Memory allocation -Garbage collection.
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
Informationsteknologi Wednesday, October 3, 2007Computer Systems/Operating Systems - Class 121 Today’s class Memory management Virtual memory.
Dynamic Memory Allocation II
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)
Lecture 7 Page 1 CS 111 Summer 2013 Dynamic Domain Allocation A concept covered in a previous lecture We’ll just review it here Domains are regions of.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
External fragmentation in a paging system Use paging circuitry to map groups of noncontiguous free pages into logically contiguous addresses (remap your.
LINKED LISTS.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Chapter 17 Free-Space Management
Section 10: Memory Allocation Topics
Memory Management Memory Areas and their use Memory Manager Tasks:
Chapter 2: The Linux System Part 4
Top 50 Data Structures Interview Questions
Partitioned Memory Allocation
Lecture - 6 On Data Structures
Dynamic Domain Allocation
Dynamic Memory Allocation
Memory Management Memory Areas and their use Memory Manager Tasks:
Main Memory Management
LINKED LISTS CSCD Linked Lists.
Optimizing Malloc and Free
CS Introduction to Operating Systems
Computer Architecture
Chapter 12 Memory Management
Memory Management (1).
Data Structures and Algorithms
Presentation made by Steponas Šipaila
Memory Management Memory Areas and their use Memory Manager Tasks:
Data Structures and Algorithms
COMP755 Advanced Operating Systems
Presentation transcript:

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 management also deals with external fragmentation: The heap is divided into small pieces sandwiched within allocated chunks internal fragmentation: memory allocated is larger than requested. 8k 12k 22k 18k31k 5k 43k

Memory Management Suppose that we have a request for 12KB and the manager allocated 16K for that request Because of internal fragmentation, 4K are wasted green areas are allocated memory 12k is actually used 4k wasted 32k8k32k16k

Memory Management Basics The memory manager should support the following two operations: acquire() and release(). Acquire: which locates a region of contiguous, unused memory locations of a specified size and returns a pointer to that region. The region is marked reserved. Release: which returns a region of reserved memory locations to the heap.

Free List Implementation: Singly Linked Lists A data structure plus implementation of acquire() and release() operations over this data structure is needed. Chosen implementation should consider speed (of memory allocation and deallocation) as the most important metric. One implementation of free list is based on Linked Lists. length next :::: length next :::: length next :::: length next ::::...

Implementation with Singly Linked List (cont'd) The acquire() method is called when memory is requested. It takes as a parameter the size of the requested memory It returns a reference to the allocated memory. The acquire() method should search the free list for a free area. IF an area of larger size than the requested was found, split into two areas, allocate one and insert back the other. There could be many nodes in the free list that satisfy the request. The one to be chosen depends on the searching policy.

Implementation with Singly Linked List (cont'd) Example. A request is received: p= aquire(20); Assume that we use first-fit sequential search

Implementation with Singly Linked List (cont'd) release() operation The release() operation returns reclaimed memory to heap. In addition to freeing memory, release() also combines free memory areas with contiguous addresses to generate larger areas. Memory areas in the free list should be kept sorted by address so that they we can combine adjacent memory areas.

Implementation with Singly Linked List (cont'd) Example. Release() method: Suppose that the same memory area we allocated in the previous slide is released now. Note that our linked list should be sorted according to the starting address of the free blocks, this is the only way to allow adjacent nodes to be combined

Implementation with Singly Linked List (cont'd) The singly-linked free list should be kept sorted The running time of the release() operation is O(n) in the worst case, where n is the number of nodes in the list. One problem with singly-linked free list is that, given a pointer to an area, we cannot extract that area from the free list without traversing the list because in order to extract an element from a linked list, we need to know the predecessor of that element.

Implementation using Doubly Linked List To overcome the problems of singly linked list implementation we can use doubly linked list length next prev status : length next prev status : length next prev status : length next prev status : head.....

Implementation: Doubly Linked List (cont'd) Release()To release a free area, we just insert the free area node at the head of the free list length next prev status : length next prev status : length next prev status : length next prev status : head.....

Implementation: Doubly Linked List (cont'd) The acquire() method must traverse the free list using any sequential allocation strategy ( first-fit/best-fit/worst fit/next-fit). While acquire() is searching for a memory area, the adjacent memory areas are combined. When an area (node) is visited, the area that immediately follows that area is examined. If free it should be combined with the area corresponding to the visited node.

Implementation: Doubly Linked List (cont'd) We can know if the adjacent memory area is free by checking the status field. If the adjacent area is free, we combine the sizes of the two areas into an area of the size equal the sum of the sizes of the two areas and we extract the other node Keep combining adjacent nodes until you reach a reserved node.

Implementation: Doubly Linked List (cont'd) Length = 15 next Length = 20 Length next prev Status = reserved Status = free Status = reserve } } head … Suppose that we want to allocate 15KB using the first fit strategy.

Comparing the Two Implementations Both of them use acquire() and release() and both should consider the segmentation problem by combining adjacent memory areas. For both implementations, we found that the acquire() method has complexity of O(n). For doubly linked list we could have release() method to be of O(1) but it is O(n) for singly linked list. One advantage of doubly linked list is that we don't need to maintain the linked list sorted, which means that small segments are subject to accumulate at the beginning of the free list

Buddy Systems We describe two storage pool implementations that both use a linear list to keep track of the free areas. The singly- linked list should be sorted by address. For doubly-linked, the order of the areas is random. When allocating memory, the free lists are searched to find an area that is sufficiently large to satisfy the request. Which is of complexity O(n) for the worst case. In Buddy Systems, only limited range of sizes can be allocated. It maintains many free lists of memory areas of given allowed sizes.

Buddy Systems (cont'd) In a buddy system, we can only allocate areas of sizes powers of two. When a request is made for an area of size 2^k we first look in the corresponding free list. If there are no areas of that size left, we can obtain one by splitting an area of size 2^(k+1) in two. If there are no areas of size 2^(k+1) left, try 2^(k+2) and so on. Whenever an area is freed, we check to see if its buddy is also free. If free, combine them to generate a larger area and insert in the next larger list. Otherwise insert it in its list.

Buddy Systems (cont'd) The buddy of any block of length 2^k is determined by complementing the (k+1)th bit Example: assume that we have a memory of length 8 For blocks of size 4, the difference in address is in the third bit

Buddy Systems: Allocation Algorithm If block of size (N) is requested, allocate a block of size 2^k where k = ceil(log N). Examine the free list avail[k] If the block of that size is available Remove a block from this list. If a block of size 2^k is unavailable, try a block of size 2 x 2^k split it into (buddies) one for the request, one for the free list of sizes k blocks. If no block of size 2 x 2^k is available, repeat for a block of size 4 x 2^k and so on.

Buddy Systems: Release Algorithm (cont'd) When a block is to be released, check whether its buddy is free If its buddy is free the two buddies should be combined Repeat combining into larger buddies recursively, until no more buddies can be combined.

Buddy System: Example Suppose that the allocated 16KB is returned

Buddy Systems: Adv. and Disadv. + Fast memory allocation: all what is needed is a simple recursive splitting operations. + Fast memory release and fast combining of free memory areas. - Internal fragmentation: specially if the requested size is slightly larger than a power of 2. - External fragmentation: because we can only combine a free block if its buddy is free.