The Illusion of Infinite Memory
Mohit Mishra
Engineering @ Amadeus || X-Omdena || JGEC 2023 || KWoC 2021 || GWOC 2021
In my previous post about the magic of page tables, we explored how virtual memory allows us to run programs larger than our available RAM. But what happens when the demand for virtual memory exceeds the physical RAM available?
That's where swapping comes into play, and it can sometimes lead to a more problematic situation known as thrashing.
Swapping
When RAM is full, the operating system starts swapping less frequently used pages of memory out to a designated area on the hard disk called the "swap space" or "swap file".
This frees up RAM for more active processes. When those swapped-out pages are needed again, they're brought back into RAM.
Thrashing: The Performance Nightmare
While swapping is a necessary mechanism, it can lead to a performance bottleneck known as thrashing. Thrashing occurs when the system spends more time swapping pages in and out of memory than actually executing instructions.
This happens when the demand for memory significantly exceeds the available RAM. Imagine constantly having to fetch books from the library because your desk is too small - you'd spend more time traveling than reading!
领英推荐
Tradeoffs of Swapping:
Why is Thrashing So Slow?
Disk access is significantly slower than RAM access. Think of RAM as your desk, where you can quickly grab the things you need. The hard drive is like a library across town - retrieving information takes a lot longer. Constant swapping introduces significant delays, making the system feel sluggish and unresponsive.
How to Avoid Thrashing?