# Lecture 10 - Memory: Wrap Up & Review Source: https://wisconsin.twango.dev/sp26-cs537/lectures/lecture-10 # Lecture 10 - Memory: Wrap Up & Review **Instructor:** Michael Swift **Topics:** Page replacement, working set, thrashing, swap space, memory allocation, memory-mapped files, review > [!abstract]- Learning Outcomes > - Understand how to use page tables to share data efficiently > - Understand how the kernel accesses user data > - Understand how swapping works > - Apply swapping policies to determine the number of page faults --- ## Page Replacement Recap Which page in main memory should be selected as victim? - Write out victim page to disk if modified (dirty bit set) - If victim page is not modified (clean), just discard What is the **best possible** choice? **OPT:** Replace page **not used for longest time in future** - Advantages: Guaranteed to minimize number of page faults - Disadvantages: Requires that OS predict the future; not practical, but good for comparison --- ## Working Set What is the right amount of memory to give a process? - All memory allocated - What it is using this cycle > [!note] Lecture Note > A sequential access pattern needs only 1 or 2 pages. A random access pattern needs more pages proportional to D (the window size). > **Working set:** The working set for a process at time t, W(D,t), is the set of pages that have been referenced in the last D virtual time units. - **Virtual time** = time elapsed while the process was in execution (e.g., number of instructions executed) - D is a window of time - At any t, |W(D,t)| is non-decreasing with D - W(D,t) is an approximation of the program's locality ### Working Set and Locality Working set model assumes locality. > **Principle of locality:** A program clusters access to data and text temporally. As the number of physical pages exceeds a threshold, page fault rate drops dramatically. > [!note] Lecture Note > How much time is spent faulting? Could be up to 50%. At 10ms per fault, that's 50 faults/sec. At 50us per fault, that's 10,000 faults/sec. ### The Working Set Strategy The working set of a process first grows when it starts executing, then stabilizes by the principle of locality. It grows again when the process enters a new locality (transition period) — up to a point where the working set contains pages from two localities — then decreases after a sufficient long time spent in the new locality. How to determine sizes: - Monitor page fault rate - High rate -> needs more memory - Low rate -> has enough > [!note] Lecture Note > When the fault rate is low, you can also remove memory from the process. --- ## Thrashing Computation has locality. As page frames decrease, the page frames available are not large enough to contain the locality of the process. The processes start **faulting heavily**. Pages that are read in are used, then immediately paged out. ### Thrashing and CPU Utilization - As page fault rate goes up, processes get suspended on page out queues for the disk - System may try to optimize performance by starting new jobs - Starting new jobs will reduce the number of page frames available to each process, increasing the page fault requests - System throughput plunges **Solution?** Stop running some processes. --- ## Swap Space Kernel organizes regions of virtual memory as "areas" or segments according to how they are swapped: - Data that gets swapped to the same file all goes to a segment - Multiple memory areas can get swapped to the same file in different places, or to anywhere in a "swap file" or "swap partition" How do you find a place to swap a page in the swap file? - Swap daemon maintains a **swap map** of: - Which blocks on disk are in use - Which virtual pages are stored in those blocks > [!note] Lecture Note > The swap map uses a bitmap to track which blocks are in use, and maps disk addresses to pages. --- ## Memory Allocation **Option 1:** Grow the heap - `brk()` — set end of heap - `sbrk()` — adjusts end of heap - Downside: can't free memory in the middle **Option 2:** Create new regions - `mmap(MAP_ANONYMOUS)` — creates a new memory region - Used by `malloc()` for new regions - `munmap(vaddr, len)` — frees range of addresses --- ## Memory-Mapped Files Memory-mapped file I/O allows file I/O to be treated as routine memory access by **mapping** a disk block to a page in memory. A file is initially read using demand paging. A page-sized portion of the file is read from the file system into a physical page. Subsequent reads/writes to/from the file are treated as ordinary memory accesses. Simplifies file access by treating file I/O through memory rather than `read()` / `write()` system calls. Also allows several processes to map the same file, allowing the pages in memory to be shared. ### How `mmap` Uses Paging > **mmap flow:** > > 1. `mmap()` maps a file to a region of the virtual address space > 2. OS creates page table entries for the mapped region marked as "backed" by the file > 3. On first access, a page fault occurs > 4. The page fault handler reads the file contents from disk into a physical page > 5. The page table is updated and the instruction retries > 6. Subsequent accesses read file contents directly from memory ### Sharing Through Mapped Files Multiple processes can map the same file into their own virtual address spaces. The OS maps their virtual pages to the **same physical pages** in memory, so both processes share the same data without duplication. --- ## Summary: Virtual Memory **Abstraction:** Virtual address space with code, heap, stack **Address translation:** - Contiguous memory: base, bounds, segmentation - Using fixed-size pages with page tables **Challenges with paging:** - Extra memory references: avoid with TLB - Page table size: avoid with multi-level paging, inverted page tables, etc. **Larger address spaces:** Swapping mechanisms, policies (LRU, Clock) --- ## Review Questions ### Limited Direct Execution Most computers have a privileged mode where extra instructions are available. Give an example of a privileged instruction, and explain what would happen if that instruction was not privileged (i.e., normal user-mode code could execute the instruction). > [!note] Lecture Note > Examples: change trap table, modify MMU (PTBR, segments, base/bounds). If these were not privileged, a user process could redirect system calls or access other processes' memory. ### Processes A process is a running program. What structures are in the memory of a process that are not in the program file? > [!note] Lecture Note > Stack and heap — these are allocated at runtime, not stored in the program file. ### Scheduling When is first-come, first-served scheduling preferable to round-robin scheduling and why? > [!note] Lecture Note > When context switches are expensive, or when you want to optimize turnaround time (FCFS avoids the overhead of frequent switching). ### Page Tables What is the benefit of multi-level paging over a single level page table? What is one drawback? > [!note] Lecture Note > Benefit: saves space (don't need entries for unused portions of the address space). Drawback: more memory accesses on a TLB miss (must walk multiple levels). ### Page Replacement Suppose you have a choice of two different page replacement policies. One performs better (i.e., fewer page faults) but uses more CPU time to decide what to evict, and you choose this one. If you now run both systems on a machine with a much faster disk, would you make the same choice? > [!note] Lecture Note > No — with a faster disk, the cost of a page fault is lower, so the benefit of fewer faults is reduced. The simpler policy that uses less CPU time may now be preferable. ### Memory Allocation A user can allocate memory by calling `malloc()` and free it by calling `free()`. When a user doesn't call `free()`, it is called a memory leak. When is it **OK** to have a memory leak in your program? > [!note] Lecture Note > - If the program terminates soon (OS reclaims all memory) > - If the allocation is mapped to disk (large, file-backed) > - If the allocation is constant, smallish size (bounded leak) ### LRU Page Replacement LRU is considered a good replacement policy. Describe what a good replacement policy should do, and why LRU is good. ### Shortest Job First Scheduling Describe a use of computers where **Shortest Job First** scheduling would be a good choice. What information is needed by the OS to use this scheduling, and how can it get this information? ### Interrupts Timer interrupts are a useful mechanism for the OS. Why? ### Process Creation Write some code for a Unix-based system to launch a new child process that executes the program "whoami" with no arguments and to then wait for the child process to complete. --- ## Related - **Navigation:** [[lecture-09|← Lecture 9]] · [[lecture-11|Lecture 11 →]] - **Exam:** [[exams/midterm-1/index|Midterm 1]] - **Course:** [[README|CS 537 Map of Content]]