System Software: 1. Memory Management 2. Garbage Collection 3. Linkers and Loaders 4. Debuggers 5. Text Editors
System Software: 1. Memory Management 2. Garbage Collection 3. Linkers and Loaders 4. Debuggers 5. Text Editors
1. Memory Management
2. Garbage Collection
3. Linkers and Loaders
4. Debuggers
5. Text Editors
Marks obtained by end-term exam
http://ssw.jku.at/Misc/SSW/
1. Memory Management
1.1
1.2
1.3
1.4
1.5
1.6
Overview
Allocation and deallocation of memory
Single free list
Multiple free lists
Buddy system
Memory fragmentation
stack
local variables
heap
p = q;
1. Memory Management
1.1
1.2
1.3
1.4
1.5
1.6
Overview
Allocation and deallocation of memory
Single free list
Multiple free lists
Buddy system
Memory fragmentation
top
top
a
top
m = mark();
a = alloc(size);
allocates a block
of size size and
returns its address a
free(m);
Advantage
Disadvantage
void free(int m) {
top = m;
}
a = alloc(size);
deallocation of a block
dealloc(a);
heap
free
(free list)
1. Memory Management
1.1
1.2
1.3
1.4
1.5
1.6
Overview
Allocation and deallocation of memory
Single free list
Multiple free lists
Buddy system
Memory fragmentation
heap
free
used block
length
free block
length
data
next
unused
Allocation of blocks
p = alloc(size);
Best Fit
returns the block with the smallest waste (blocks are split)
blockj: lengthj >= (size+4) && i j: lengthi >= lengthj
+ minimum waste
- slow (must traverse the whole list; good if the list is sorted by block size)
Next Fit
10
len next
unused
len
Splitting
p = alloc(size);
unused
len' next
used
len''
p
len'
size
len''
The block is cut off from the end => next does not have to be changed
If less than 8 bytes would remain the whole block is used
11
free
prev
free
len
0 len
next
size
p
free
newLen
0 nL
next
free
len
1 len
size
12
Deallocation of blocks
dealloc(p);
free
left
right
Requires sequential traversals of the heap and the free list => slow
13
Boundary tags
In order to avoid the sequential searching of blocks
length and used bit are also stored at the block end (also in used blocks)
0 len next prev
0 len
right neighbor of p:
left neighbor of p:
successor of p in free list:
predecessor of p in free list:
p + p.len
p - (p-8).len
p.next
p.prev
There must be a used dummy block at the beginning and the end of the heap.
14
Lazy merge
dealloc(p) does not merge free blocks with their neighbors
free
p
p.next = free;
free = p;
free
free
15
1. Memory Management
1.1
1.2
1.3
1.4
1.5
1.6
Overview
Allocation and deallocation of memory
Single free list
Multiple free lists
Buddy system
Memory fragmentation
16
Allocation of blocks
Observation
90% of all blocks have only one of 5 different sizes
...
...
Blocks in the last list have different sizes, but their size is a multiple of 1024 bytes
17
Algorithms
Block alloc (int size) {
if (size > 512) {
p = suitable block from the last list (e.g. first fit);
put remaining i*1024 byte back to the last list;
} else {
int s = 8, n = 3;
// free[0..2] are dummies
while (s < size) { s = 2 * s; n++; } // n = log2(size);
if (free[n] == null) split(n+1);
p = free[n]; free[n] = p.next;
// p = first block from free[n]
}
initialize block p with 0;
return p;
}
0
1
2
3
4
5
6
7
8
9
10
8
8
8
16
16
32
64
128
256
512
2048
4096
Example
Allocation of 40 bytes (given the follwing free lists)
8
16
32
64
128
256
512
n*1024
8 8 8
16 16
32
2048
4096
Split a 1024 byte block and use it to fill the empty lists
8 8 8
16 16
32
2048
4096
8 8 8
16 16
32
8 8 8
16 16
32
8 8 8
16 16
32
512 512
1024 4096
256 256
512
1024 4096
128 128
256
512
1024 4096
8 8 8
16 16
32
64 64
128
256
512
1024 4096
19
Deallocation of blocks
void dealloc (Block p) {
int s = 8, n = 3;
while (s < p.len) { s = 2 * s; n++; } // n = log2(p.len);
if (n > 10) n = 10;
p.next = free[n]; free[n] = p;
// add p to free[n]
}
Blocks are not merged, because a block of the same size is probably needed soon again
(maybe one should do a lazy merge from time to time)
20
16
8
2i, 3*2i
16
16
k * 16
16
64
32
24
8
32
16
+
-
32
64
32
48
16
128
16
128
48
16
64
256
256
64
16
80
16
512
512
96
32
96
16
1024
128
32
112
16
64
128
16
192
256 ...
64
144
16
160 ...
16
Example
we need a block with 40 bytes
search the list containing blocks of size 32..63;
we find a block of size 58, say
split the block into 40 + 18 bytes
add 18-byte block into list with sizes 16..31
+
+
-
no internal fragmentation
the lists are shorter than with a single free list
the list must be searched for a suitable block (e.g. next fit)
leads to many small blocks of waste
free blocks should be merged lazily from time to time
22
1. Memory Management
1.1
1.2
1.3
1.4
1.5
1.6
Overview
Allocation and deallocation of memory
Single free list
Multiple free lists
Buddy system
Memory fragmentation
23
8 8
16 16
32 32
64 64
as described above
...
dealloc(p)
Every block has a partner (buddy) of the same size
p
q
p is the buddy of q and vice versa
32 32
A block can only be merged with its buddy
The address of p's buddy can be computed from the address of p
Buddy addresses
Buddies emerge from splitting blocks of size 2i
Assume that we have a block of size 64 (= 26) at address 0
0
64
32
32
x
32
y
adr(x) = 0
=00000000
adr(y) = 32
=00100000
size = 25
48
16
x
16
y
adr(x) = 32
=00100000
adr(y) = 48
=00110000
size = 24
adr(x) = 32
=00100000
adr(y) = 40
=00101000
size = 23
A block of size 2i has an address which is a multiple of 2i (it has i zeroes at the end)
25
p
heap
p
if p is deallocated,
multiple blocks are
merged
26
1. Memory Management
1.1
1.2
1.3
1.4
1.5
1.6
Overview
Allocation and deallocation of memory
Single free list
Multiple free lists
Buddy system
Memory fragmentation
27
internal fragmentation
unused bytes within
an allocated block
28
Compaction
Requires 2 traversals of the heap and 1 traversal through all pointers
10
70
130
heap
pointers
70
30
30
130
50
50
10
0
30
50
70
30
130
50
30
30
50
50
29
30
master pointers
pointers
master[]
20 30
Step 2: Compact
20
30
master[]
a = heapStart;
for (all blocks p) {
if (p.used) {
i = p.len; p.len = master[i]; master[i] = a;
move block p to a;
a = a + p.len;
}
}
30