Ubuntu 20.04添加swap交换空间
由于经常使用虚拟机中安装Ubuntu系统下载编译开发安卓源码,经常遇到编译安卓源码过程中内存不足的情况,导致编译安卓源码失败。由于不能再给虚拟机分配更多的物理内存,只能考虑增加ubuntu swap交换分区来解决内存不足的情况。
1.dd命令创建交换空间文件
qiang@ubuntu:~$sudo dd if=/dev/zero of=/home/qiang/myswapfile/swap bs=1024 count=10485760
2.更改交换空间用户访问权限
qiang@ubuntu:~$sudo chmod 600 /home/qiang/myswapfile/swap
3.使用mkswap命令创建交换空间
qiang@ubuntu:~$sudo mkswap /home/qiang/myswapfile/swap
4.启动交换空间
qiang@ubuntu:~$sudo swapon /home/qiang/myswapfile/swap
以上swapon只是临时启动交换空间,重启系统之后就失效了。如果需要开机启动自动激活。需要将交换分区启动配置写到/etc/fstab文件中。
qiang@ubuntu:~$sudo vim /etc/fstab
添加内容以后的fstab文件内容如下:
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda5 during installation
UUID=d881cd51-8235-4e88-86f4-28c632dfe928 / ext4 errors=remount-ro 0 1
# /boot/efi was on /dev/sda1 during installation
UUID=2F29-D844 /boot/efi vfat umask=0077 0 1
# /swapfile none swap sw 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0
/home/qiang/myswapfile/swap none swap sw 0 0
~
5.查看交换分区
执行如下命令:
qiang@ubuntu:~$
qiang@ubuntu:~$ free -m
total used free shared buff/cache available
Mem: 10341 919 10616 3 804 10121
Swap: 10239 0 10239
qiang@ubuntu:~$