# 系統調整，儘量不使用swap

**linux kernel 決定是否要將 ram 裡的資料搬到 swap 的策略**

swappiness 的值介於 0 \~ 100 之間, 值愈高, kernel 愈會將 application 用的記憶體搬到 swap。悲慘的是, 預設值一直都是 60, 但現在的電腦有很大的 ram (早期 512MB vs. 現在最少 4GB), 用 60 的結果是, 明明還有很多記憶體空間可用, 一搬大檔案, file cache 達到記憶體臨界值, kernel 就將 application 用的記憶體搬到 swap, 於是莫明奇妙整個系統進入緩慢的狀態。

如果記憶體使用達到臨界值，就會使用swap，設定的 60 太高，所以要充分使用記憶體，就調低swapnesss 值。

sysctl -a

vi /etc/sysctl.conf

```
vm.swappiness=10
```

sysctl -p 寫入

echo 3 > /proc/sys/vm/drop\_caches

swapoff -a

swapon -a

### 寫入 1 會清除 pagecache：

#### echo 1 > /proc/sys/vm/drop\_caches

### 寫入 2 會清除 dentries 與 inodes：

#### echo 2 > /proc/sys/vm/drop\_caches

### 寫入 3 則有等同於 1+2 的效果，會清除 pagecache、dentries 及 inodes：

#### echo 3 > /proc/sys/vm/drop\_caches


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kawsing.gitbook.io/opensystem/andoid-shou-ji/untitled-4/xi-tong-diao-zheng-jin-liang-bu-shi-yong-swap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
