8.Memory Management§

Memory Manager ADT

// Memory Manager abstract class
interface MemManager {
  // Store a record and return a handle to it
  public MemHandle insert(byte[] info);

  // Release the space associated with a record
  public void release(MemHandle h);

  // Get back a copy of a stored record
  public byte[] getRecord(MemHandle h);
}

Implementation Issues

Dynamic Storage Allocation

Fragmentation

Managing the Free Blocks

Selecting a Free Block

Sequential Fit Methods

First Fit: Start from beginning, pick first free block that is big enough
Store list in memory-pool order
Circular first fit: Move forward from current position
Best Fit: Pick the smallest block big enough
Store by block size, or search list
Protect large blocks for big requests
Worst Fit: Pick the biggest block
Store by block size, or search list
Avoid external fragmentation

Example

.


Buddy Method

The memory pool is a power of 2 in size.
Memory allocations are always the smallest power of 2 equal to or bigger than the request.
Free (and allocated) blocks are therefore always a power of 2
Keep a list for each block size
Easy to merge freed blocks

Buddy Method Example

.


Failure Policies

What do we do if there is no free block that can hold the message?
Must resort to a failure policy.
Reject the request
Grow the memory
Compact the memory
Garbage collection