創建swap文件
- 進入
/usr
目錄
[root@localhost usr]$ pwd
/usr
[root@localhost usr]$
- 創建
swap
文件夾,並進入該文件夾
[root@localhost usr]# mkdir swap
[root@localhost usr]# cd swap/
[root@localhost swap]# pwd
/usr/swap
[root@localhost swap]#
- 創建
swapfile
文件,使用命令dd if=/dev/zero of=/usr/swap/swapfile bs=1M count=4096
[root@localhost swap]# dd if=/dev/zero of=/usr/swap/swapfile bs=1M count=4096
記錄了4096+0 的讀入
記錄了4096+0 的寫出
4294967296字節(4.3 GB)已複製,15.7479 秒,273 MB/秒
[root@localhost swap]#
查看swap文件
- 使用命令
du -sh /usr/swap/swapfile
,可以看到我們創建的這個swap文件爲4g
[root@localhost swap]# du -sh /usr/swap/swapfile
4.1G /usr/swap/swapfile
[root@localhost swap]#
將目標設置爲swap分區文件
- 使用命令
mkswap /usr/swap/swapfile
將swapfile文件設置爲swap分區文件
[root@localhost swap]# mkswap /usr/swap/swapfile
mkswap: /usr/swap/swapfile: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=5bd241ff-5375-449d-9975-5fdd429df784
[root@localhost swap]#
激活swap區,並立即啓用交換區文件
- 使用命令
swapon /usr/swap/swapfile
[root@localhost swap]# swapon /usr/swap/swapfile
[root@localhost swap]#
- 使用命令
free -m
來查看現在的內存,可以看到裏面的Swap分區變成了4095M,也就是4G內存。
[root@localhost swap]# free -m
total used free shared buffers cached
Mem: 980 910 70 3 8 575
-/+ buffers/cache: 326 654
Swap: 4095 0 4095
[root@localhost swap]#
設置開機自動啓用虛擬內存,在etc/fstab
文件中加入如下命令
-
使用vim編輯器打開/etc/fstab文件
-
在文件中加入如下內容
/usr/swap/swapfile swap swap defaults 0 0
使用reboot命令重啓服務器
- 輸入
reboot
命令來重啓
[root@localhost swap]# reboot
Broadcast message from [email protected]
(/dev/pts/1) at 3:56 ...
The system is going down for reboot NOW!
[root@localhost swap]# Connection to 192.168.136.142 closed by remote host.
Connection to 192.168.136.142 closed.
[進程已完成]
- 重啓完成過後使用free -m 命令來查看現在的內存是否掛在上了。
[root@localhost swap]# free -m
total used free shared buffers cached
Mem: 980 910 70 3 8 575
-/+ buffers/cache: 326 654
Swap: 4095 0 4095