27 - Memory ManagementOutlineProcesses in Physical MemoryRecap: Processes and MemoryAnnouncementsReading:SwappingMemory FragmentationProcesses in Physical MemoryEach process has memoryit must exist in.No abstractionsAddress spacesVirtual memoryImproved pagingSwappingManaging Free MemoryPagingTranslation lookaside bufferShares physical memory with kernelNo isolation! program may tamper with kernelKernel acts more like a librarye.g. MSDOS, many embedded systemsMultilevel page tablesHow does process memory get implemented onphysical memory?Program is loaded directly into physical memory andhas direct access.Multiple programs with no abstractions, limits sizeof process memory.Issue: Program locationRelocation ProblemProgram memory addressesneed to be maderelocatable.Additional informationincluded in executableNo protection (no security)Relocation expensive(rewrite every absolute memory location)Loader decides where toplace programs and mustrewrite programlocations at runtime.Compiler needs to compute address locations formemory operations. Shifting executable's locationcould break program behavior.Physical memory (RAM) is a hardware array ofwordsIncreasing physical RAM extends the array.Address Spaces1)3)4)2)MOS 3.1 - 3.2Will release midterm grades today.PA 4 due FridayNo class Friday--------------Words are hardware-defined, fixed number ofbytes (usually now 64-bit)stackdatatext...wordwordwordword...0x000000x10000No AbstractionsAddress SpacesBase and Limit (Bound) RegistersSwappingMemory FragmentationCompactionProcess Growthuserprogramuserprogramuserprogram...userprogramuserprogramprogram Buserprogramuserprogramprogram AuserprogramOSOSOSOSOSjmp 0x0Cjmp 0x0Cjmp 0x10jmp 0x10movmovaddaddcmpcmp...........................RAM LayoutRAM LayoutKernel can beloaded at lowaddresses or highaddressesalongside program.Kernel can beloaded at lowaddresses or highaddressesalongside program.0x00000x00000x00000x00000x00000x00000x10000x000C0x000C0x00100x00100x00100x10100x10000x10000x10000x2000program Aprogram BphysicalmemoryBA----absolute jumpbreaks Babsolute jumpOK in A0x1010Problems:Virtual Address SpacesFrom the previous figure, how did the loadercompute the relocated absolute addresses?MMU computes relocated addresses on-the-flyusing base and limit registersThese are saved in the PCB and loaded to theMMU during context switching.Using Base & Limit registers is an older way ofimplementing address spaces. Modern machines usea different scheme.What do we do when main memory is full?abstraction from physical memorybase register: program's loaded base addresslimit register: program's maximum sizeprograms compile relative to 0x0 addressdedicated hardware support for relocationMemory Management Unit (MMU)-----limit(program size)base(program base)process 1address0x0f000x10000x0010+yesnoOOB,raise memoryfaultaddress< limit ?MMU......partition 1partition 2partition 0physicalmemoryprogram B:jmp 0x100x00000x10000x1f00MMU performs address translationSwappingDetailsStoring the actual data can be done via file ordedicated disk partitionProcess creation,termination, and swappingcreates free gaps in memory.Swapping is performed by the kernelToo many active processes forces swapping ofprocess in ready queue, serious performancepenalties.Process becomes idle when blocked "a long time"When swapped out, process memory no longer inRAM, except for PCBnew process may not fitin an available gap, even ifthere is enough total freememoryavailable memory become"fragmented"arrange process tightly tohave one large free gaphope new gap largeenough for new processsave address space of idle process to disk andreclaim memory (swap out process)when ready to run, load address space backfrom disk (swap in process)-------------Linux: swap partition/etc/fstabsudo fdisk -lWindows: C:\swapfile.sys$ sudo fdisk -lDisk /dev/sda: 64 GiB, 68719476736 bytes, 134217728 sectorsDisk model: QEMU HARDDISK Units: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x2ae68c61Device Boot Start End Sectors Size Id Type/dev/sda1 * 2048 127299583 127297536 60.7G 83 Linux/dev/sda2 127301630 134215679 6914050 3.3G f W95 Ext'd (LBA)/dev/sda5 127301632 134215679 6914048 3.3G 82 Linux swap / SolarisBBBBBBDDDCCCCCAAAAAAAOSOSOSOSOSOSOSdiskprocess D creationprocess A readyRAMMOS Figure 3-4BBBCCCDDAAAOSOSOS?fragmentationWhat can be done to resolve this?Rearrange processes in memoryWhy is this expensive?What if a process needs more space?What if a running process needs more space?Pre-allocate extra room for process growth.(dynamic allocation on stack or heap)but how much room?where should room for growth go?what if the process still needs more memory?tradeoffs between memory usage and out-of-memory riskReality looks quite different with paging andASLRmove process to larger gap, or swap outprocess and wait for a larger gap to open upBBAheapheapstackstackAOSOSroom forgrowthroom forgrowthMOS Figure 3-5a lot of copyingswapswapif we have too many processes in RAM, fora new one, we can make room by using disk.