Memory Management & Virtual Memory
Hierarchy Cache Memory : Provide invisible speedup to main memory
Hierarchy Virtual Memory : Provide invisible extension to main memory
Terminology Virtual/Logical Address : Physical Address : Page : Address program uses Size determined by bits in memory address Physical Address : Determined by available memory Page : Block of memory 4KB+ in Windows/Linux
Memory Management Memory Management Turns Virtual addresses used by processor into Physical addresses in memory
Virtual Memory How Why? Logical space can be independent of physical space Programs don't have to care how many GB ram you have
Virtual Memory Why? Allow programs to use more main memory than is physically present Hard drive used as extra space
Virtual Memory How Why? Allows multiple programs to coexist
Virtual Memory How Why? Allows multiple programs to coexist Everyone can use same logical addresses
Virtual Memory How Why? Allows multiple programs to coexist invisibly Some memory can be shared by processes
Virtual Memory How Why? Allows multiple programs to coexist invisibly Memory Management Unit can prevent access to unowned memory
Terminology Page Table : stores physical address of every page Paging : copying page from hard drive to RAM Page Fault : accessing a virtual page not in RAM
Example Assume each page is 4KB (4096 bytes) 32KB Virtual space, 12KB Physical
Example Assume each page is 4KB (4096 bytes) CPU asks for address 100 100 bytes into page 0
Example Assume each page is 4KB (4096 bytes) CPU asks for address 100 100 bytes into page 0 Virtual page 0 is in Physical Page 2
Example Assume each page is 4KB (4096 bytes) CPU asks for address 100 100 bytes into page 0 Virtual page 0 is in Physical Page 2 Physical address is 4K * 2 + 100
Example 2 Assume each page is 4KB (4096 bytes) CPU asks for address 4296 200 bytes into page 1
Example 2 Assume each page is 4KB (4096 bytes) CPU asks for address 4296 200 bytes into page 1 Not in physical memory Page Fault
Example 2 Assume each page is 4KB (4096 bytes) CPU asks for address 4296 200 bytes into page 1 Kick a page out to drive
Example 2 Assume each page is 4KB (4096 bytes) CPU asks for address 4296 200 bytes into page 1 Load desired virtual page into physical memory Update Page Table 3
Example 2 Assume each page is 4KB (4096 bytes) CPU asks for address 4296 200 bytes into page 1 Virtual page 1 is now in Physical Page 3 Physical address is 4K * 3 + 200 3
Page Faults Page Fault = 40,000,000 cycles with HDD! 600,000 with SSD
Page Sizes Small page size More book keeping More physical pages Less thrashing
Page Sizes Large page size Less book keeping More temporal locality If we use it all Fewer physical pages More thrashing
Page Mapping Memory addresses broken into Page Offset : Enough bits to specify every address in page Page # : All the rest
Page Mapping Computer has 32 bit address space, 1GB of physical memory and 4KB pages 1GB = 230 Physical address need 30 bits
Page Mapping Computer has 32 bit address space, 1GB of physical memory and 4KB pages Pages are 212 bytes 12 bits for offset
Page Mapping Computer has 32 bit address space, 1GB of physical memory and 4KB pages Pages are 212 bytes 12 bits for offset Virtual page number = 32 – 12 = 20 bits
Page Mapping Computer has 32 bit address space, 1GB of physical memory and 4KB pages Pages are 212 bytes 12 bits for offset Virtual page number = 32 – 12 = 20 bits Physical page number = 30 – 12 = 18 bits
Process Need logical address 0000 0111 0000 1010 0011 0010 1111 0100
Process Find virtual page # and offset in page 0000 0111 0000 1010 0011 0010 1111 0100 0000 0111 0000 1010 0011 0010 1111 0100
Process Find virtual page # and offset in page 0000 0111 0000 1010 0011 0010 1111 0100 Look up in page table… 0010 1111 0100 0000 0111 0000 1010 0011
Process Find virtual page # and offset in page 0000 0111 0000 1010 0011 0010 1111 0100 Look up in page table… Get physical page # 0010 1111 0100 0000 0111 0000 1010 0011 00 0010 1111 1111 0000
Process Find virtual page # and offset in page 0000 0111 0000 1010 0011 0010 1111 0100 Look up in page table… Get physical page # Combine with offset to make physical address 0010 1111 0100 0000 0111 0000 1010 0011 00 0010 1111 1111 0000 00 0010 1111 1111 0000 0010 1111 0100
Paging & Caching Page table held in main memory by OS
Paging & Caching Page table held in main memory by OS Every mapping goes to main memory… Cache is worthless!
TLB Translation look-aside buffer Cache for page table Subset of page table Fully associative May be multiple levels