Top Banner
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. 8 k 12k 22k 18k 31k 5k 43k
22

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.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

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 18k 31k 5k 43k

Page 2: 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.

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

32k 8k 32k 16k

Page 3: 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.

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.

Page 4: 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.

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

:

:

. . .

Page 5: 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.

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 satisfythe request. The one to be chosen depends on the searching

policy.

Page 6: 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.

Implementation with Singly Linked List (cont'd)

Example.A request is received: p= aquire(20);Assume that we use first-fit sequential search.

10

5

30 20

8

Page 7: 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.

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.

Page 8: 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.

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.

25 30 20

8

10

Page 9: 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.

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.

Page 10: 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.

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

. . . . .

Page 11: 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.

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

. . . . .

Page 12: 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.

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.

Page 13: 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.

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.

Page 14: 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.

Implementation: Doubly Linked List (cont'd)Length = 15

next

Length = 20

Length

next

next

prev

prev

prev

Status = reserved

Status = free

Status = reserve

}}

head

.. ..... …

Suppose that we want to allocate 15KB using the first fit strategy.

Page 15: 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.

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

Page 16: 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.

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.

Page 17: 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.

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.

Page 18: 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.

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.

000 001 010 011 100 101 110 111

Page 19: 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.

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.

Page 20: 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.

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.

Page 21: 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.

Buddy System: Example

Suppose that the allocated 16KB is returned

1

2

4

8

16

32

64

Page 22: 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.

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.