Linux虚拟机增加内存后扩展swap空间

2020-07-03  本文已影响0人  沉思的雨季

在给Linux虚拟机增加内存之后,Linux系统不能像Windows一样自动识别新增内存,导致新增的内存无法使用到。因此,需要手动修改Linux系统的配置,在Linux虚拟机上扩展swap的空间。具体过程如下:

一、查看现有memory及swap

hadoop@slave1:~$ free -m
             total       used       free     shared    buffers     cached
Mem:          1994       1898         96          0         66        106
-/+ buffers/cache:       1725        269
Swap:         1021       1021          0

二、用dd命令创建一个新增内存大小的文件,如1GB(1G=102410241=1048576)

hadoop@slave1:~$ sudo dd if=/dev/zero of=/var/swapfile1 bs=1024 count=1048576
[sudo] password for hadoop: 
1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB) copied, 12.8365 s, 83.6 MB/s

三、将新增文件设为Swap交换分区

hadoop@slave1:~$ sudo mkswap /var/swapfile1
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=cd088b86-b1c5-4dca-84fa-a059b368bf56

四、激活并使用/var/swapfile1交换分区

hadoop@slave1:~$ sudo swapon /var/swapfile1

五、设置系统启动后自动激活交换分区

hadoop@slave1:~$ sudo vi /etc/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/sda1 during installation
UUID=4754790a-7c18-40d0-8c03-ee2dcea12a55 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=01b709d6-22cf-4518-9ad4-45544c71050e none            swap    sw              0       0
/dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0
#新增Swap交换分区配置
/var/swapfile1  swap  swap  defaults  0   0

六、重启Linux系统,使用free -m查看swap大小,使用swapon -s查看swap状态

hadoop@slave1:~$ free -m
             total       used       free     shared    buffers     cached
Mem:          3002        778       2224          3         82        438
-/+ buffers/cache:        257       2745
Swap:         2045          0       2045
hadoop@slave1:~$ swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda5                               partition       1046524 0       -1
/var/swapfile1                          file            1048572 0       -2
上一篇下一篇

猜你喜欢

热点阅读