Unit34_MemoryManagement
Unit34_MemoryManagement
the memory used by a program can be allocated from three different areas:
In languages like C or Java,
• A static area, which is laid out at compilation time, and allocated when the program starts,
• A stack, from which memory is allocated and freed dynamically, in LIFO order,
• A heap, from which memory is allocated and freed dynamically, in any order.
Location of data
Each of the areas presented before is useful to store different kinds of data:
• Global variables and constants go into the static area,
• Local variables and method parameters go into the stack,
• All data outliving the method which created them go into the heap. In Java, objects are stored in the
heap.
The memory management techniques we discuss in this lecture apply exclusively to the management
of the heap.
The memory manager is part of the Operating System. It must keep track of which parts of the heap
are free, and which are allocated. To keep track of free memory a memory manager uses a data
structure called free list.
A free list is a data structure that keeps track of free memory blocks in a scheme for dynamic memory
allocation.
Memory manager:
o allocates memory needed by programs.
2
A memory manager supports the following two allocation and deallocation operations:
acquire(size) : searches for a contiguous block of size bytes from the heap and returns a
reference to that block. It throws an exception if such a block does not exist.
release(address, size) : returns the indicated block to the heap for reuse
The aim of allocation is to find a free block big enough to satisfy the request, and possibly split it
in two if it is too big: one part is then returned as the result of the allocation, while the other is put
back in the free list.
On deallocation, adjacent free blocks can be coalesced (ie., combined) to form bigger free blocks.
Memory fragmentation:
o External fragmentation
Memory wasted outside allocated blocks
o Internal fragmentation
Memory wasted inside allocated block(s). Results when memory allocated is larger than
memory requested.
Overhead: Additional memory that must be allocated, above and beyond that requested by
programs, in order tp provide for the management of the heap. For example, it may require a
few words of memory to describe the location of each allocated memory block.
Allocation policies
Whenever a block of memory is requested, there will in general be several free blocks big enough
to satisfy the request. A policy must therefore be used to decide which of those candidates to choose.
There are several such policies: first fit, next fit, best fit, worst fit, etc.
First fit chooses the first block in the free list big enough to satisfy the request, and split it.
Next fit is like first fit, except that the search for a fitting block will start where the last one stopped,
instead of at the beginning of the free list.
Best fit chooses the smallest block bigger than the requested one.
Worst fit chooses the biggest, with the aim of avoiding the creation of too many small fragments – but
doesn’t work well in practice.
3
4
where the shaded areas are allocated blocks. The corresponding free-list is:
The operation acquire(700) using the first-fit allocation policy will result in:
5
The operation release(400, 1100) , i.e., release 400 bytes starting at address 1100, will result in:
The release operation does not combine adjacent free blocks. It simply prepends a node
corresponding to a freed block at the front of the free list. This operation is thus O(1). Adjacent
free blocks are combined by acquire().
6
The acquire operation traverses the free list in order to find a free area of a suitable size. As it
does so it also combines adjacent free blocks.
Boundary Tags
Boundary tags are data structures on the boundary between blocks in the heap from which storage is
allocated. The use of such tags allows blocks of arbitrary size to be used, they also make it easier to
coalesce (i.e., combine) adjacent free blocks. The tag describes for each block, how big it is, its status
(allocated or free), and the addresses of its neighbours.
Note: We may also include the status (allocated or free) of each of the three blocks in the node.
where the shaded areas are allocated blocks and the grayed areas are boundary tags. The corresponding
free-list is:
7
Notice that the adjacent free blocks starting from address 3200 to 4700 are not combined at this stage.
The node corresponding to the freed block is appended at the front of the free-list:
The nodes x, y, and z correspond to the three free blocks that have not yet been combined.
The operation acquire(600) using the first-fit allocation policy will first result in the combination of
the three adjacent free blocks:
8
o The heap is viewed as one large block which can be split into two equal smaller blocks,
called buddies. Each of these smaller blocks can again be split into two equal smaller
buddies, and so on. Each memory block has its “buddy”. The “buddy” of a block of size 2k
that starts at address x is the block of size 2k that start at address y = complementBit_k(x),
where the address bits are numbered from right to left starting with 0.
Example: If each block is of size 8 bytes (i.e., 2 3 bytes); then the buddy of a block is
obtained by complementing bit 3 of its starting address. If each block is of size 4 bytes (i.e.,
22 bytes); then the buddy of a block is obtained by complementing bit 2 of its starting
address.
10
11
Example: What is the starting address of the buddy of a block that starts at address
1100101010101101 if each block is 16 bytes?
Solution: 16 = 24; the starting address of the buddy is obtained by complementing bit 4:
1100101010111101
When a request is made for a block of size x <= 2k, a search is made in the corresponding free list. If
there is a block in this list, it is allocated; otherwise if there are no free blocks of size 2 k, we can obtain
one by splitting a block of size 2 k+1 in two. And if there are no blocks of size 2 k+1, we can obtain one of
those by splitting a block of size 2 k+2 in two, and so on. If we reach the heap size without obtaining an
appropriate free block; the request cannot be satisfied; an exception is thrown. When a block is split
into two buddies, it is taken off the free list of its size. One buddy is placed on the free list for the next
lower size, and the other is used, splitting it again if needed.
When a block of memory is of size 2k is released, it is placed back in the free list of its size, and if its
buddy is also free they are combined to form a free block of size 2k+1. This block is then moved to the
corresponding free list. If its buddy is free they are combined to form a free block of size 2k+2, which is
then moved to the appropriate free list and so on.
Example: Assume the size of an initially empty heap is 256K. The corresponding free-list is:
12
Suppose the memory manager issues the following requests in Kilobytes: acquire(9), acquire(32),
acquire(30), release(30), release(9), release(32) in the given order:
1. acquire(9)
There are no free blocks of size 16, 32, 64, and 128. The block of size 256 is split into two;
these two blocks are attached to the free-list of blocks of size 128:
One of the blocks of size 128 is split into two; the resulting two blocks of size 64 are attached to
the free-list of blocks of size 64:
13
One of the blocks of size 64 is split into two; the resulting two blocks of size 32 are attached to the
free-list of blocks of size 32:
One of the blocks of size 32 is split into two; the resulting two blocks of size 16 are attached to the
free-list of blocks of size 16:
One block of size 16 is allocated; since the request was for 9KB; 7KB are wasted in internal
fragmentation:
14
2. acquire(32)
3. acquire(30)
There is no free block of size 32. The free block of size 64 at the free-list of blocks of size 64 is
split into two; the resulting blocks of size 32 are moved to the free-list of blocks of size 32:
One of the 32KB blocks is allocated; since the request was for 30KB, 2KB are wasted in internal
fragmentation:
16
4. release(30)
The buddy of the released 32KB block is free. The two blocks are combined to form a block of
size 64:
5. release(9)
The buddy of the released 16KB block is free. The two blocks are combined to form a block of
size 32:
6. release(32)
The buddy of the released 32KB block is free. The two blocks are combined to form a block of
size 64:
The buddy of the 64KB block is free. The two blocks are combined to form a block of
size 128; this is then attached to its free-list:
19
The buddy of the 128KB block is free. The two blocks are combined to form a block of
size 256; this is then attached to its free-list: