You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

操作系统交换空间:数据何时存入swap space及运行时作用?

Swap Space: What It Actually Does (And What It Doesn’t)

Great question—swap space is one of those system components that’s easy to confuse with regular disk storage, so let’s clear this up step by step.

First, let’s directly address your two core questions:

  • Swap space is NOT where programs store all their data at execution time. Programs always load their active data into RAM first—swap doesn’t get touched until RAM is under pressure.
  • Its primary role is as temporary backup for "page-outs", and it’s where those pages live until they need to be swapped back into RAM (a "page-in") when the program needs them again.

Let’s break down the full workflow to make this concrete:

  1. Normal execution: When you run a program, the OS loads the necessary data and code from disk into RAM (this is what you’re describing when you say "main memory reads data from disk if it’s not in cache/RAM"). Swap isn’t involved here at all.
  2. RAM runs low: Once your system’s RAM is nearly full, the OS uses a "page replacement algorithm" to identify memory pages that haven’t been used recently (like data from a background app you haven’t touched in an hour). It moves these inactive pages from RAM to swap space—freeing up RAM for the active programs you’re actually using.
  3. Page-in when needed: If you later switch back to that background app, the OS needs those pages again. It will swap the inactive pages currently in RAM (if needed) back to swap, then load the required pages from swap into RAM so the app can resume.

A quick bonus use case: Swap space is also used when your system goes into hibernation. The OS writes the entire contents of RAM to swap, then shuts down. When you boot back up, it reads that data from swap back into RAM, letting you pick up right where you left off. But this is a special scenario, not the day-to-day use.

To sum it up: Swap is RAM’s overflow parking lot—you only use it when the main lot is full, and you move cars (memory pages) back and forth as needed.

内容的提问来源于stack exchange,提问作者hannibal1296

火山引擎 最新活动