linux 添加 swap

1)在linux下,首先,查看内存和swap大小:
[root@rhel6 usr]# free -m
             total       used       free     shared    buffers     cached
Mem:          1386       1272        114          0         90        952
-/+ buffers/cache:        228       1158
Swap:         2047          0       2047
 
2)创建swap文件:
[root@rhel6 usr]# mkdir swap
[root@rhel6 usr]# cd swap/
[root@rhel6 swap]# dd if=/dev/zero of=swapfile bs=402400 count=10000
 
记录了10000+0 的读入
记录了10000+0 的写出
4024000000字节(4.0 GB)已复制,224.925 秒,17.9 MB/秒
 
[root@rhel6 swap]# ls -al
总用量 3929700
drwxr-xr-x.  2 root root       4096 2月   4 10:16 .
drwxr-xr-x. 15 root root       4096 2月   4 10:15 ..
-rw-r--r--.  1 root root 4024000000 2月   4 10:20 swapfile
 
[root@rhel6 swap]# mkswap swapfile
mkswap: swapfile: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 3929680 KiB
no label, UUID=4e2ce2fe-3963-46ce-90cd-466495cd8ca8
 
3)激活swap文件:
[root@rhel6 swap]# swapon swapfile
[root@rhel6 swap]# free -m
             total       used       free     shared    buffers     cached
Mem:          1386       1296         90          0         82        975
-/+ buffers/cache:        239       1147
Swap:         5885          0       5885
 
4)若想要开机时自启动,需要修改文件/etc/fstab中的swap行:
[root@rhel6 swap]# vi /etc/fstab 
UUID=8c6bea6a-3bba-4594-b5a8-fd4afd311476 swap                    swap    defaul
ts        0 0
/usr/swap/swapfile                                                swap    defaul
ts        0 0
 
/dev/zero,是一个输入设备,你可你用它来初始化文件,可以用于向设备或文件写入NULL(二进制的0流)

/dev/zero主要的用处是用来创建一个指定长度用于初始化的空文件,就像临时交换文件.

关于 /dev/zero 的另一个应用是为特定的目的而用零去填充一个指定大小的文件, 如挂载一个文件系统到环回设备 (loopback device) 或"安全地" 删除一个文件

转自:http://blog.sina.com.cn/s/blog_633685790101dqh9.html
http://www.codesky.net/article/201105/170022.html
原文地址:https://www.cnblogs.com/yorkyang/p/6640834.html