工作笔记-增加swap分区空间

有时编译Android全代码会报错空间不足,可以通过增加swap空间来避免

查看Swap空间大小

caihuijian@caihuijian:~$ free -m
              total        used        free      shared  buff/cache   available
Mem:           7868        1144        5495         352        1228        6095
Swap:          8075           0        8075
caihuijian@caihuijian:~$ 
caihuijian@caihuijian:~$ sudo mkdir /swap
[sudo] caihuijian 的密码: 
caihuijian@caihuijian:~$ cd /swap/

在原有swap基础上增加8G空间

caihuijian@caihuijian:/swap$ sudo dd if=/dev/zero of=swapfile bs=1G count=8

记录了8+0 的读入
记录了8+0 的写出
8589934592 bytes (8.6 GB, 8.0 GiB) copied, 45.0893 s, 191 MB/s

创建一个 Swap 文件

caihuijian@caihuijian:/swap$ sudo  mkswap /swap/swapfile 
Setting up swapspace version 1, size = 8 GiB (8589930496 bytes)
无标签, UUID=179cba9a-944b-458a-9e1c-c924ad578662
caihuijian@caihuijian:/swap$ 

尝试激活 Swap 文件

caihuijian@caihuijian:/swap$ swapon /swap/swapfile
swapon: /swap/swapfile:不安全的权限 0644,建议使用 0600swapon: /swap/swapfile:swapon 失败: 不允许的操作
caihuijian@caihuijian:/swap$ 
caihuijian@caihuijian:/swap$ 

使用root用户操作(不是必要)

caihuijian@caihuijian:/swap$ su -
密码: 

尝试激活 Swap 文件

root@caihuijian:~# swapon /swap/swapfile
swapon: /swap/swapfile:不安全的权限 0644,建议使用 0600swapon: /swap/swapfile:swapon 失败: 设备或资源忙
root@caihuijian:~# 

修改文件权限

root@caihuijian:~# chmod 0600 /swap/swapfile 
root@caihuijian:~#

尝试激活 Swap 文件

root@caihuijian:~# swapon /swap/swapfile
swapon: /swap/swapfile:swapon 失败: 设备或资源忙
root@caihuijian:~# 

激活 Swap 文件

root@caihuijian:~# swapoff /swap/swapfile 
root@caihuijian:~# swapon /swap/swapfile
root@caihuijian:~# 
root@caihuijian:~# 

再次查看swap空间大小

root@caihuijian:~# free -m
              total        used        free      shared  buff/cache   available
Mem:           7868        1178        1174         371        5516        5987
Swap:         16267           0       16267
root@caihuijian:~# 

重启计算机后查看swap分区,居然又退回8G了

caihuijian@caihuijian:~$ free -m

              total        used        free      shared  buff/cache   available
Mem:           7868         561        6639         153         668        6891
Swap:          8075           0        8075
caihuijian@caihuijian:~$ 

重新激活swap分区

caihuijian@caihuijian:~$ sudo swapon /swap/swapfile
[sudo] caihuijian 的密码: 
caihuijian@caihuijian:~$ 
caihuijian@caihuijian:~$ free -m
              total        used        free      shared  buff/cache   available
Mem:           7868         879        6100         229         889        6489
Swap:         16267           0       16267
caihuijian@caihuijian:~$ 
caihuijian@caihuijian:~$ 

如果需要一直保持这个 swap ,可以把它写入 /etc/fstab 文件

caihuijian@caihuijian:~$ sudo vim /etc/fstab


#增加以下两行:

(add swap space on /swap/swapfile)
  /swap/swapfile                          /swap           swap    defaults        0       0


caihuijian@caihuijian:~$ 
caihuijian@caihuijian:~$ 
caihuijian@caihuijian:~$ free -m
              total        used        free      shared  buff/cache   available
Mem:           7868         937        5979         283         951        6376
Swap:         16267           0       16267
caihuijian@caihuijian:~$ 
##################关机重启确认Swap大小
caihuijian@caihuijian:~$ free -m
              total        used        free      shared  buff/cache   available
Mem:           7868         914        6074         220         879        6463
Swap:         16267           0       16267
caihuijian@caihuijian:~$ 

你可能感兴趣的:(工作笔记)