4 Memory
4 Memory
COMS(3010A)
Memory
Branden Ingram
[email protected]
Office Number : ???
Recap
• Interrupt Stack
• System Calls
• UpCalls
• UNIX
Memory Virtualization
Current
Program
(code, data, etc.)
max
Physical Memory
Multiprogramming and Time Sharing
0KB
Operating System
• Load multiple processes in memory (code, data, etc.)
• Execute one for a short while
64KB
• Switch processes between them in memory
Free
• Increase utilization and efficiency
128KB
Process A
• Cause an important protection issue
(code, data, etc.)
• Errant memory accesses from other processes
192KB
Process C
(code, data, etc.)
256KB
Free
320KB
Process B
(code, data, etc.)
448KB
Free
max
Physical Memory
Address Space
Free
15KB
Stack
16KB
Address Space
Address Space
• Code 0KB
• Where instructions live
• Heap Program Code
• Dynamically allocate memory 1KB
• “malloc” in C language Heap
• “new” in object-oriented language 2KB
• Stack
• Store return addresses or values
• Contain local variables arguments to Free
routines
15KB
Stack
16KB
Address Space
Address Space
0KB
Operating System
(code, data, etc.) 0KB
Process A Heap
(code, data, etc.) 30KB
192KB
Process C
(code, data, etc.) Free
256KB
Free
60KB
320KB Stack
Process B
64KB
(code, data, etc.)
448KB Address Space
Free
max
Physical Memory
Well how do we map between Physical
Memory and the Address Space?
return x;
}
Free
0x7fff9ca45fcc
Stack
0x7fff9ca45fcc
Address Space
Address Translation
• C - Language code
void func()
int x;
...
x = x + 3; // this is the line of code we are interested in
• Assembly
• Presume that the address of ‘x’ has been place in ebx register
• Load the value at that address into eax register
• Add 3 to eax register
• Store the value in eax back into memory
Address Translation - Example
Free
60KB
61KB 3000 Stack
64KB
Physical Memory
What happens if the OS needs to relocate a
process?
Process A Heap
(code, data, etc.) 30KB
192KB
Process C
(code, data, etc.) Free
256KB
Free
60KB
320KB Stack
Process B
64KB
(code, data, etc.)
448KB Address Space
Free
max
Physical Memory
Memory Relocation
0KB
Operating System
0KB (code, data, etc.)
64KB
Program Code
Free
Relocated Process
15KB 128KB
Heap Process A
30KB (code, data, etc.)
192KB
Process C
(code, data, etc.)
Free 256KB
Free
60KB
Stack 320KB
Process B
64KB (code, data, etc.)
Address Space 448KB
Free
max
Physical Memory
Base and Bounds Registers
0KB
Operating System 128KB
0KB (code, data, etc.)
base register
64KB
Program Code
Free
Relocated Process
15KB 128KB
Heap Process A
30KB (code, data, etc.)
192KB
Process C
(code, data, etc.)
Free 256KB
Free
60KB
Stack 320KB
Process B
64KB (code, data, etc.)
Address Space 448KB
64KB Free
max
bounds register
Physical Memory
Hardware base Relocation
• Every virtual address must not be greater than bound and negative
Free
189KB = 61KB + 128KB(base)
60KB
61KB 3000 Stack
64KB
Address Space
Two ways of Bounds Register
0KB
Operating System 128KB
0KB (code, data, etc.)
base register
64KB
Program Code
Free
15KB 128KB
Relocated Process
Heap Program Code
192KB
30KB Heap
bound register
Free
Free Stack
192KB
Process C
256KB (code, data, etc.)
60KB
Free
Stack 320KB
Process B
64KB (code, data, etc.)
Address Space 448KB
64KB Free
max
bounds register
Physical Memory
What are the Issues an OS must handle for
Memory Virtualizing?
Process A
Heap
Free
64KB
Stack
192KB
Process C
256KB (code, data, etc.)
256KB
Free
320KB
Process B
(code, data, etc.)
448KB
448KB Free
max
Physical Memory
OS Issues: When a Process Is Terminated
0KB
Free list 0KB
Free list
Operating System Operating System
(code, data, etc.) (code, data, etc.)
64KB 64KB
Free 64KB 64KB
128KB
Process C
Free
(code, data, etc.)
256KB
Free
320KB 256KB 320KB 448KB
Process B Process B
(code, data, etc.) (code, data, etc.)
448KB 448KB
Free Free
max max
448KB
Physical Memory Physical Memory
OS Issues: When Context Switch Occurs
Process B PCB
• The OS must save and restore the base-and-bounds pair.
…
• In process structure or process control block(PCB) base : 320KB
bounds : 448KB
…
0KB
Operating System 0KB
(code, data, etc.) Operating System
64KB (code, data, etc.)
Free 64KB base register
128KB Free 128KB
Process C
Context Switch 128KB
(code, data, etc.) Process C
bounds register
256KB Currently Running
Free
base register 256KB 256KB
320KB 320KB Free
Process B 320KB
Currently Running bounds register Process B
448KB (code, data, etc.)
448KB
448KB
Free
max Free
max
Physical Memory
Physical Memory
What if there is not enough space together
to fit a program?
Free
60KB
Stack
64KB
Address Space
Segmentation
Free
60KB
Stack
64KB
Address Space
Segmentation
0KB
Operating System
• Segment is just a contiguous portion of the address (code, data, etc.)
space of a particular length
64KB
• Logically-different segment: code, stack, heap
Free
128KB
• Each segment can be placed in different part of Stack
physical memory 192KB
• Base and bounds exist per each segment Free
256KB
Segment Register Code
260KB
Segment Base Size Heap
Code 256KB 4KB 320KB
Heap 260KB 60KB
Stack 128KB 64KB
Free
max
Physical Memory
Address Translation with Segmentation
0KB 128KB
Program Code Code
15KB 143KB
16000 data
640 + 143KB
Heap Heap
30KB 158KB
Free
60KB Free
Stack
64KB 260KB
Address Space Physical Memory
What happens if an address is incorrectly
referenced?
• If an illegal address such as 37KB which is beyond the end of heap is referenced,
the OS occurs segmentation fault
• The hardware detects that address is out of bounds
0KB
Program Code
15KB
Heap
30KB
Free
60KB
Stack
64KB
Address Space
Referring to a Segment
• Explicit approach
• Chop up the address space into segments based on the top few bits of virtual
address
13 12 11 10 9 8 7 6 5 4 3 2 1 0
Segment Offset
Referring to a Segment
• Explicit approach
• Chop up the address space into segments based on the top few bits of virtual
address
13 12 11 10 9 8 7 6 5 4 3 2 1 0
Segment Offset
Segment bits
13 12 11 10 9 8 7 6 5 4 3 2 1 0
Code 00
Heap 01 0 1 0 0 0 0 0 1 1 0 1 0 0 0
Stack 10
- 11 Segment Offset
Referring to a Segment
1 // get top 2 bits of 14-bit VA
2 Segment = (VirtualAddress & SEG_MASK) >> SEG_SHIFT
3 // now get offset
4 Offset = VirtualAddress & OFFSET_MASK
5 if (Offset >= Bounds[Segment])
6 RaiseException(PROTECTION_FAULT)
7 else
8 PhysAddr = Base[Segment] + Offset
9 Register = AccessMemory(PhysAddr)
• SEG_MASK = 0x3000(11000000000000)
• SEG_SHIFT = 12
• OFFSET_MASK = 0xFFF (00111111111111)
• VA = 01100011100011
• Segment = 01000000000000 >> 12 = 01
• Offset = 00100011100011
Referring to a Stack Segment
• External Fragmentation: little holes of free space in physical memory that make
difficulty to allocate new segments
• There is 24KB free, but not in one contiguous segment
• The OS cannot satisfy the 20KB request
OS support: Fragmentation
• External Fragmentation: little holes of free space in physical memory that make
difficulty to allocate new segments
• There is 24KB free, but not in one contiguous segment
• The OS cannot satisfy the 20KB request
Free
max max
Physical Memory Physical Memory
Free-Space Management
• Splitting
• Coalescing
• Keeping Track of Memory Region Sizes
• Strategies for Managing Memory
Splitting
• Finding a free chunk of memory that can satisfy the request and splitting it
into two
Splitting
• Finding a free chunk of memory that can satisfy the request and splitting it
into two
• For Example : When request for memory allocation is smaller than the
size of free chunks.
Splitting
• For Example : When request for memory allocation is smaller than the size of
free chunks.
• Two 10-byte free segments with a 1-byte request
Addr:0 Addr:20
Free list: head Len:10 Len:10 NULL
Before Splitting
Splitting
• For Example : When request for memory allocation is smaller than the size of
free chunks.
• Two 10-byte free segments with a 1-byte request
Addr:0 Addr:21
Free list: head Len:10 Len:9 NULL
• Merge returning a free chunk with existing chunks into a large single free
chunk if addresses of them are nearby
Coalescing
• Merge returning a free chunk with existing chunks into a large single free
chunk if addresses of them are nearby
• For Example : When request for memory that is bigger than free chunk
size, the list will not find such a free chunk
Coalescing
• For Example : When request for memory that is bigger than free chunk size,
the list will not find such a free chunk
• Three 10-byte free segments with a 20-byte request
Before Coalescing
Addr:0
Free list: head Len:30 NULL
After Coalescing
Coalescing
• For Example : When request for memory that is bigger than free chunk size,
the list will not find such a free chunk
• Three 10-byte free segments with a 20-byte request
Before Coalescing
Addr:0
Free list: head Len:30 NULL
After Coalescing
Coalescing
• For Example : When request for memory that is bigger than free chunk size,
the list will not find such a free chunk
• Three 10-byte free segments with a 20-byte request
Before Coalescing
Coalescing
• For Example : When request for memory that is bigger than free chunk size,
the list will not find such a free chunk
• Three 10-byte free segments with a 20-byte request
Before Coalescing
After Coalescing
Coalescing
• For Example : When request for memory that is bigger than free chunk size,
the list will not find such a free chunk
• Three 10-byte free segments with a 20-byte request
Before Coalescing
After Coalescing
How do we do this reorganisation in an
efficient way
• By tracking the size of allocated regions
• By embedding the free list
Tracking the size of allocated regions
• How does the library know the size of memory region that will be back
into free list?
Tracking the size of allocated regions
• How does the library know the size of memory region that will be back
into free list?
• Most allocators store extra information in a header block
ptr = malloc(20);
• The header minimally contains the size of the allocated memory region
• The header may also contain
Additional pointers to speed up deallocation
A magic number for integrity checking
ptr = malloc(20);
hptr
Size = 20
typedef struct __header_t {
Magic = 12345 int size;
ptr
The 20 bytes int magic;
returned by } header_t;
caller
An Allocated Region Plus Header Simple Header
The header
• The size for required free region is the size of the header plus the size of the
space
allocated to the user.
• If a user request N bytes, the library searches for a free chunk of size N
plus the size of the header
The header
• The size for required free region is the size of the header plus the size of the
space
allocated to the user.
• If a user request N bytes, the library searches for a free chunk of size N
plus the size of the header
• The memory-allocation library initializes the heap and puts the first
element of the free list in the free space
• The memory-allocation library initializes the heap and puts the first
element of the free list in the free space
• The memory-allocation library initializes the heap and puts the first
element of the free list in the free space
• The memory-allocation library initializes the heap and puts the first
element of the free list in the free space
hptr
Size = 20
Magic = 12345 Allocated Block
ptr
hptr
Size = 20
Next = 0 Free Block
Embedding a Free List : Allocation
• This is done by iterating through the free list and checking the size
Embedding a Free List : Allocation
• This is done by iterating through the free list and checking the size
hptr
Size = 4088
Next = 0 A 4KB Heap
With 1 Free Chunk
Embedding a Free List : Allocation
Size = 100
Magic = 12345
sptr
Size = 100
Magic = 12345
Size = 3764
Next = 0
Size = 100
Next = 16708
sptr
Size = 3764
Next = 0
Size = 100
Next = 16708
sptr
Size = 3764
Next = 0
• Best Fit:
• Finding free chunks that are big or bigger than the request
• Returning the one of smallest chunks in the group of candidates
Managing Free Space : Basic Strategies
• Best Fit:
• Finding free chunks that are equal or bigger than the request
• Returning the one of smallest in the chunks in the group of candidates
• Worst Fit:
• Finding the largest free chunks and allocating the amount of the request
• Keeping the remaining chunk on the free list
Managing Free Space : Basic Strategies
• First Fit:
• Finding the first chunk that is big enough for the request
• Returning the requested amount and the remaining rest of the chunk
Managing Free Space : Basic Strategies
• First Fit:
• Finding the first chunk that is big enough for the request
• Returning the requested amount and the remaining rest of the chunk
• Next Fit:
• Finding the first chunk that is big enough for the request.
• Searching at where one was looking at instead of the begging of the list
Basic Strategies - Examples
• Result of Best-fit
• Result of Best-fit
• Result of Worst-fit
• The allocator divides free space by two until a block that is big enough
to accommodate the request is found
Managing Free Space : Buddy Allocation
• The allocator divides free space by two until a block that is big enough
to accommodate the request is found
64 KB
32 KB 32 KB
16 KB 16 KB
8 KB 8 KB