Ubuntu 18.04 開啟 swapfile
開啟 swapfile 的動機在於,本機喚醒時間太慢,嘗試解決這個問題。
本機已經 鎖屏休眠時間 設置為 Never,但是喚醒還得5-6秒。懷疑是不是本機適用的是硬盤休眠,所以喚醒慢;理想的狀態是內存睡眠,喚醒更快一些。
注意區分兩個概念
- 睡眠 Suspend to RAM,簡稱 STR,比如 Intel 的 modern standby
- 休眠 Suspend to Disk,簡稱 STD
吐槽一句,休眠這個名字太迷惑了,和睡眠根本分不清,不如叫“冬眠”更合適。
查看 /sys/power/state 得到:
freeze mem disk
可以看出,本機其實是支持 STR 和 STD 兩種節能模式的。
但是我沒找到怎么設置使用 STR 的地方,那就先看看 STD 怎么搞快一點吧。畢竟,明明用的固態硬盤,喚醒還得好幾秒,太不像話。
本機有32G內存,查看swap,sudo swapon --show 得到:
NAME TYPE SIZE USED PRIO
/dev/sda3 partition 977M 7.3M -2
可以看到是使用的交換分區,並且分區遠小於內存大小。這可能會對休眠功能造成影響,我們接下來將它擴大到和內存一樣大。
使用 swapfile 相比於 swap partition 的好處是,關閉交換之后,可以像普通文件一樣直接 rm 刪除。
參考 Linuxize的這篇博客,我們來新增一個32G的交換文件。
以下是全文轉載。
How to Add Swap Space on Ubuntu 18.04
Updated Feb 6, 2020
Swap is a space on a disk that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, inactive pages are moved from the RAM to the swap space.
Swap space can take the form of either a dedicated swap partition or a swap file. Generally when running Ubuntu on a virtual machine, a swap partition is not present, and the only option is to create a swap file.
This tutorial covers the steps necessary to add a swap file on Ubuntu 18.04 systems.
Before You Begin
Before continuing with this tutorial, check if your Ubuntu installation already has swap enabled by typing:
$ sudo swapon --show
If the output is empty, it means that your system does not have swap space enabled.
Otherwise, if you get something like below, you already have swap enabled on your machine.
NAME TYPE SIZE USED PRIO
/dev/sda2 partition 1.9G 0B -2
Although possible, it is not common to have multiple swap spaces on a single machine.
Creating a Swap File
The user you are logged in as must have sudo privileges to be able to activate swap. In this example, we will add 1G swap. If you want to add more swap, replace 1G with the size of the swap space you need.
Perform the steps below to add swap space on Ubuntu 18.04.
- Start by creating a file which will be used for swap:
$ sudo fallocate -l 1G /swapfile
If fallocate is not installed or you get an error message saying fallocate failed: Operation not supported then use the following command to create the swap file:
$ sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
- Only the root user should be able to write and read the swap file. Set the correct permissions by typing:
$ sudo chmod 600 /swapfile
- Use the mkswap utility to set up a Linux swap area on the file:
$ sudo mkswap /swapfile
- Activate the swap file using the following command:
$ sudo swapon /swapfile
To make the change permanent open the /etc/fstab file:
$ sudo nano /etc/fstab
and paste the following line:
/swapfile swap swap defaults 0 0
- Verify that the swap is active by using either the swapon or the free command , as shown below:
$ sudo swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 507.4M -1
$ sudo free -h
total used free shared buff/cache available
Mem: 488M 158M 83M 2.3M 246M 217M
Swap: 1.0G 506M 517M
Adjusting the Swappiness Value
Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible, while a higher value will make the kernel to use the swap space more aggressively.
The default swappiness value is 60. You can check the current swappiness value by typing the following command:
$ cat /proc/sys/vm/swappiness
60
While the swappiness value of 60 is OK for most Linux systems, for production servers, you may need to set a lower value.
For example, to set the swappiness value to 10, run:
$ sudo sysctl vm.swappiness=10
To make this parameter persistent across reboots, append the following line to the /etc/sysctl.conf file:
vm.swappiness=10
The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.
Removing a Swap File
To deactivate and remove the swap file, follow these steps:
- Start by deactivating the swap space by typing:
$ sudo swapoff -v /swapfile
- Next, remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.
- Finally, remove the actual swapfile file using the rm command:
$ sudo rm /swapfile
Conclusion
You have learned how to create a swap file and activate and configure swap space on your Ubuntu 18.04 system.
If you hit a problem or have feedback, leave a comment below.